1、存储基本数据类型的数组结构
public class TestPrimitiveArray {
public static void main(String[] args) {
//1.定义数组
int[] numbers;
//2.分配内存空间
numbers = new int[4];
//3.为数组元素指定值
for(int i = 0;i < numbers.length;i++) {
numbers[i] = i * 10;
}
}
}
![java数组存储](https://i-blog.csdnimg.cn/blog_migrate/0b37ac1816d232048e344994b83250b9.jpeg)
2、存储引用数据类型的
class Person {
public int age;
public String name;
public void display() {
System.out.println(name + "的年龄是: " + age);
}
}
public class TestReferenceArray {
public static void main(String[] args) {
//1.定义数组
Person[] persons;
//2.分配内存空间
persons = new Person[2];
//3.为数组元素指定值
Person p1 = new Person();
p1.age = 28;
p1.name = "Miracle";
Person p2 = new Person();
p2.age = 30;
p2.name = "Miracle He";
persons[0] = p1;
persons[1] = p2;
//输出元素的值
for(Person p : persons) {
p.display();
}
}
}
![java数组存储](https://i-blog.csdnimg.cn/blog_migrate/784c58ce6799ac223109d4c13329c963.jpeg)
跟我想象中一样。。。╮(╯▽╰)╭