Java集合的嵌套遍历(单列集合)

本文介绍了Java中ArrayList集合存储自定义对象的遍历方式,包括Iterator、listIterator、普通for循环和增强for循环。接着讨论了集合的嵌套遍历,特别是在处理多个班级集合ArrayList<ArrayList<Student>>时如何遍历每个班级的学生信息。同时,文章还通过一个需求实例展示了如何生成10个1~20之间不重复的随机数,涉及Random类和ArrayList的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

 

1.ArrayList集合存储自定义对象并遍历,有几种方式?

 *     Iterator iterator() ;
 *     listIterator listiterator();(可以不写)
 *     普通for循环:size()和get(int index)相结合;
 *     增强for循环 ;

public static void main(String[] args) {
		
		//创建ArrayList集合
		ArrayList<Student> array = new ArrayList<Student>() ;
		
		//创建学生对象
		Student s1 = new Student("张三", 20) ;
		Student s2 = new Student("李四", 19) ;
		Student s3 = new Student("王五", 22) ;
		Student s4 = new Student("赵六", 26) ;
		
		//添加元素
		array.add(s1) ;
		array.add(s2) ;
		array.add(s3) ;
		array.add(s4) ;
		
		//方式1:迭代器遍历:Iterator 
		Iterator<Student> it = array.iterator() ;
		while(it.hasNext()) {
			Student s = it.next() ;
			System.out.println(s.getName()+"----"+s.getAge());
		}
		System.out.println("-------------------");
		//方式2:普通for
		for(int x = 0 ; x < array.size() ; x ++) {
			Student s = array.get(x) ;
			System.out.println(s.getName()+"----"+s.getAge());
		}
		
		System.out.println("-------------------");
		//方式3:增强for循环
		for(Student s : array) {
			System.out.println(s.getName()+"----"+s.getAge());
		}
	}

2.集合的嵌套遍历

 

 

要弄明白集合的存储类型是什么,对象是谁,变量是谁...

[需求1]

有一个Java基础班,里面有很多学生,把一个班级集合:ArrayList<Student> 
不止一个Java基础班,这个时候就定义大集合:ArrayList<ArrayList<Student>>

有三个基础班,分别遍历每一个班里面学生信息:(name,age) 

public static void main(String[] args) {
		
		//创建一个大的集合
		ArrayList<ArrayList<Student>> bigArrayList = new ArrayList<ArrayList<Student>>() ;
		
		//创建第一个班的集合对象
		ArrayList<Student> firstArray = new ArrayList<Student>() ;
		//创建学生对象
		Student s1 = new Student("曹操",35) ;//后知后觉
		Student s2 = new Student("诸葛亮", 29);//先知先觉
		Student s3 = new Student("蒋干", 30) ;//不知不觉
		
		//给第一个班添加元素
		firstArray.add(s1) ;
		firstArray.add(s2) ;
		firstArray.add(s3) ;
		
		//将第一个集合添加到大集合中
		bigArrayList.add(firstArray) ;
		
		//创建第二个班集合对象
		ArrayList<Student> secondArray = new ArrayList<Student>() ;
		Student s11 = new Student("宋江",38) ;
		Student s22 = new Student("鲁智深", 29);
		Student s33 = new Student("武松", 30) ;
		
		secondArray.add(s11) ;
		secondArray.add(s22) ;
		secondArray.add(s33) ;
		
		//添加到大集合
		bigArrayList.add(secondArray) ;
		
		//创建第三个集合对象
		ArrayList<Student> thirdArray = new ArrayList<Student>() ;
		Student s111 = new Student("高圆圆",38) ;
		Student s222 = new Student("唐嫣", 29);
		Student s333 = new Student("刘若英", 30) ;
		
		thirdArray.add(s111) ;
		thirdArray.add(s222) ;
		thirdArray.add(s333) ;
		//添加到集合
		bigArrayList.add(thirdArray) ;
		
		//增强for遍历:大集合
		for(ArrayList<Student> array: bigArrayList) {
			for(Student s:array) {
				System.out.println(s.getName()+"---"+s.getAge());
			}
		}
	}

 

[需求2]求10个1~20之间的随机数,要求数据不能重复 

分析:
 *  可以使用数组,数组的长度固定,不够好,使用集合去做
 *
 * 创建一个随机数类对象Random 
 * 获取指定的随机数的范围:
 *        int nextInt()  ;  获取的int类型的范围
 *        int nextInt(int n) ;获取1~n之间的数据 ,[0,n)    
 * 创建一个ArrayList集合对象,元素类型:Integer类型
 * 定义统计遍历:count  是从0开始
 * 判断如果统计遍历小于10
 *       先计算出1-20之间的随机数,还需要判断集合中是否包含这些元素
 *       如果不包含,添加,count++
 *       包含,重复,不搭理了....
 * 大于10,不搭理
 *
 * 遍历即可

public static void main(String[] args) {
		//创建随机数类对象
		Random r = new Random() ;
		
		//创建一个ArrayList集合
		ArrayList<Integer> array = new ArrayList<Integer>() ;
		
		//定义统计变量
		int count = 0 ;
		
		//判断统计变量的个数
		while(count <10) {
			//个数符合的情况下,计算出1-20之间的随机数
			int number = r.nextInt(20) +1 ;
			
			//需要判断集合总是发包含这个number
			if(!array.contains(number)) {
				array.add(number) ;
				count ++ ;
			}
		}
		
		//增强for
		for(Integer i : array) {
			System.out.println(i);
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值