package com.day11.集合框架概述;
import com.day11.bean.Student;
public class Demo1Array {
/**
* @param args
* 15.01_集合框架(对象数组的概述和使用)
* A:案例演示
* 需求:我有5个学生,请把这个5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。
*
Student[] arr = new Student[5]; //存储学生对象
arr[0] = new Student("张三", 23);
arr[1] = new Student("李四", 24);
arr[2] = new Student("王五", 25);
arr[3] = new Student("赵六", 26);
arr[4] = new Student("马哥", 20);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
* B:画图演示
* 把学生数组的案例画图讲解
*数组和集合存储引用数据类型,存的都是地址值
*/
public static void main(String[] args) {
//int[] arr=new int[5];//创建基本类型数组
Student[] arr=new Student[5];//创建引用数据类型数组
arr[0]=new Student("科比",39);//new一个具体的对象,将对象赋值给arr[0]
arr[1]=new Student("KG",41);
arr[2]=new Student("皮尔斯",40);
arr[3]=new Student("雷阿伦",42);
arr[4]=new Student("乔丹",54);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]+" ");//必须要重写Student类中父类Object类里的toString方法
}
}
}
package com.day11.bean;
public class Student {
private String name;
private int age;
public Student() {
super();
// 构造器
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name="+name+",age="+age+"]";
}
}