JAVA全系列 教程
3762个小节阅读:7088.3k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
type[ ] arr_name; //方式一
type arr_name[ ]; //方式二
注意事项
- 声明的时候并没有实例化任何对象,只有在实例化数组对象时,JVM才分配空间,这时才与长度有关。
- 声明一个数组的时候并没有数组真正被创建。
- 构造一个数组,必须指定长度。
【示例】创建基本类型一维数组
xxxxxxxxxx
public class Test {
public static void main(String args[ ]) {
int[ ] s; // 声明数组;
s = new int[10]; // 给数组分配空间;
for (int i = 0; i < 10; i++) {
s[i] = 2 * i + 1;//给数组元素赋值; 数组是对象,数组中的元素就是对象的属性
System.out.println(s[i]);
}
}
}
【示例】创建引用类型一维数组
xclass Man{
private int age;
private int id;
public Man(int id,int age) {
super();
this.age = age;
this.id = id;
}
}
public class AppMain {
public static void main(String[ ] args) {
Man[ ] mans; //声明引用类型数组;
mans = new Man[10]; //给引用类型数组分配空间;
Man m1 = new Man(1,11);
Man m2 = new Man(2,22);
mans[0]=m1;//给引用类型数组元素赋值;
mans[1]=m2;//给引用类型数组元素赋值;
}
}
实时效果反馈
1. 如下是数组的定义和初始化的案例代码,最终数组s
中的元素是:
xxxxxxxxxx
int[ ] s; // 声明数组;
s = new int[3]; // 给数组分配空间;
for (int i = 0; i < 3; i++) {
s[i] = 2 * i ;
}
A s[0]=0
s[1]=2
s[2]=4
B s[0]=0
s[1]=0
s[2]=0
C s[0]=0
s[1]=2
s[2]=4
s[3]=6
D s[0]=0
s[1]=0
s[2]=0
s[3]=0
答案
1=>A