java:集合框架(对象数组的概述和使用)

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_24644517/article/details/82944890

* 数组和集合存储引用数据类型,存的都是地址值 

import com.heima.bean.Student;

public class Demo1_Array {
/*	 A:案例演示
	 需求:我有5个学生,请把这个5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr=new int[5];//创建基本类型数据数组
		Student[] arr1=new Student[5];//创建引用数据类型数组
		arr1[0] = new Student("张三",23);//创建一个学生对象,存储在数组的第一个位置
		arr1[1] = new Student("李四",24);
		arr1[2] = new Student("王五",25);
		arr1[3] = new Student("赵六",26);
		arr1[4] = new Student("马哥",20);
		for (int i = 0; i < arr1.length; i++) {//需要Student重写toString方法,否则打印的是对象本身
			System.out.println(arr1[i]);
		}
		
	}

}

猜你喜欢

转载自blog.csdn.net/qq_24644517/article/details/82944890