集合(13):List集合练习2
集合(13):List集合练习2
1、集合的嵌套遍历
/*
需求:
阿伟机构有十三期和十四期,十四期有很多学生,每个学生都是一个学生对象,
可以用一个集合表示一个班有45个人
十四期的学生:ArrayList<Student> classList14
十三期的学生:ArrayList<Student> classList13
无论是十三期还是十四期,都是阿伟机构的班级
阿伟机构也可以用集合表示,将ArrayList<Student>插入进去
表示为:ArrayList<ArrayList<Student>> awei
这样的现象叫做集合的嵌套
*/
第1步:定义一个学生类
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
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 +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
}
第2步方式1:使用for循环遍历
import java.util.ArrayList;
public class ListQianTaoDemo {
public static void main(String[] args) {
//定义一个十四期班级的集合(加上学生泛型)
ArrayList<Student> classList14 = new ArrayList<>();
//定义一个十三期班级的集合(加上学生泛型)
ArrayList<Student> classList13 = new ArrayList<>();
//定义一个阿伟机构集合
ArrayList<ArrayList<Student>> awei = new ArrayList<>();
//将十四期和十三期添加到阿伟机构中
awei.add(classList14);
awei.add(classList13);
//创建十四期的学生对象
Student s1 = new Student("李玉伟", 18);
Student s2 = new Student("李玉刚", 19);
Student s3 = new Student("李宇春", 20);
Student s4 = new Student("李易峰", 21);
Student s5 = new Student("李嘉诚", 22);
//将学生对象添加到14期集合中
classList14.add(s1);
classList14.add(s2);
classList14.add(s3);
classList14.add(s4);
classList14.add(s5);
//创建十三期的学生对象
Student s11 = new Student("小花", 18);
Student s22 = new Student("小黄", 19);
Student s33 = new Student("小白", 17);
Student s44 = new Student("小黑", 20);
Student s55 = new Student("小红", 17);
//将学生对象添加到13期集合中
classList13.add(s11);
classList13.add(s22);
classList13.add(s33);
classList13.add(s44);
classList13.add(s55);
//集合有了,数据有了,开始遍历
//增强for循环遍历
for(ArrayList<Student> clazz : awei){ //外层for循环获取每个班级
for(Student s : clazz){ //内层fou循环获取每一个学生对象
System.out.println(s);
/*
输出结果为:
Student{name='李玉伟', age=18}
Student{name='李玉刚', age=19}
Student{name='李宇春', age=20}
Student{name='李易峰', age=21}
Student{name='李嘉诚', age=22}
Student{name='小红', age=17}
Student{name='小花', age=18}
Student{name='小黄', age=19}
Student{name='小白', age=17}
Student{name='小黑', age=20}
*/
}
}
//使用增强for循环,输出的结果没有将学生进行班级分类
//使用普通for循环,加入判断
for(int i=0;i<awei.size();i++){
if(i==0){//机构集合就两个元素,索引值为0的时候,就表示第一个元素:14期
System.out.println("==========十四期:==========");
for(int j=0;j<awei.get(i).size();j++){
Student student = awei.get(i).get(j);//awei.get(i).get(j).var
System.out.println(student);
}
}else if(i==1){//索引值为1的时候,就表示第二个元素:13期
System.out.println("==========十三期:==========");
for(int j=0;j<awei.get(i).size();j++){
Student student = awei.get(i).get(j);
System.out.println(student);
}
}
}
/*
j<awei.get(i).size()
通过下标索引获取机构集合中的班级集合的学生个数
awei.get(i).get(j)
通过下标索引获取机构集合中的班级集合的学生信息
*/
}
}
执行结果为:
Student{name='李玉伟', age=18}
Student{name='李玉刚', age=19}
Student{name='李宇春', age=20}
Student{name='李易峰', age=21}
Student{name='李嘉诚', age=22}
Student{name='小花', age=18}
Student{name='小黄', age=19}
Student{name='小白', age=17}
Student{name='小黑', age=20}
Student{name='小红', age=17}
==========十四期:==========
Student{name='李玉伟', age=18}
Student{name='李玉刚', age=19}
Student{name='李宇春', age=20}
Student{name='李易峰', age=21}
Student{name='李嘉诚', age=22}
==========十三期:==========
Student{name='小花', age=18}
Student{name='小黄', age=19}
Student{name='小白', age=17}
Student{name='小黑', age=20}
Student{name='小红', age=17}
Process finished with exit code 0
第2步方式2:使用迭代器遍历
import java.util.ArrayList;
import java.util.Iterator;
public class ListQianTaoDemo {
public static void main(String[] args) {
//定义一个十四期班级的集合(加上学生泛型)
ArrayList<Student> classList14 = new ArrayList<>();
//定义一个十三期班级的集合(加上学生泛型)
ArrayList<Student> classList13 = new ArrayList<>();
//定义一个阿伟机构集合
ArrayList<ArrayList<Student>> awei = new ArrayList<>();
//将十四期和十三期添加到阿伟机构中
awei.add(classList14);
awei.add(classList13);
//创建十四期的学生对象
Student s1 = new Student("李玉伟", 18);
Student s2 = new Student("李玉刚", 19);
Student s3 = new Student("李宇春", 20);
Student s4 = new Student("李易峰", 21);
Student s5 = new Student("李嘉诚", 22);
//将学生对象添加到14期集合中
classList14.add(s1);
classList14.add(s2);
classList14.add(s3);
classList14.add(s4);
classList14.add(s5);
//创建十三期的学生对象
Student s11 = new Student("小花", 18);
Student s22 = new Student("小黄", 19);
Student s33 = new Student("小白", 17);
Student s44 = new Student("小黑", 20);
Student s55 = new Student("小红", 17);
//将学生对象添加到13期集合中
classList13.add(s11);
classList13.add(s22);
classList13.add(s33);
classList13.add(s44);
classList13.add(s55);
//使用迭代器遍历
Iterator<ArrayList<Student>> aweiIter = awei.iterator();
while (aweiIter.hasNext()){
ArrayList<Student> clazz = aweiIter.next();
Iterator<Student> clazzIter = clazz.iterator();
while (clazzIter.hasNext()){
Student student = clazzIter.next();
System.out.println(student);
}
}
}
}
执行结果如下:
Student{name='李玉伟', age=18}
Student{name='李玉刚', age=19}
Student{name='李宇春', age=20}
Student{name='李易峰', age=21}
Student{name='李嘉诚', age=22}
Student{name='小花', age=18}
Student{name='小黄', age=19}
Student{name='小白', age=17}
Student{name='小黑', age=20}
Student{name='小红', age=17}
Process finished with exit code 0
//使用迭代器遍历,和增强for循环一样,只能遍历出信息,无法进行班级分类
总结:当使用迭代器和增强for循环遍历是时候,只能遍历出学生对象的信息,无法进行班级分类
只有当使用 普通for循环的时候,才可以遍历出学生对象的信息,并且可以班级分类
2、获取10个1-20之间的随机数,要求不能重复
import java.util.ArrayList;
import java.util.Random;
/*
数组不好实现,因为长度不好确定,我们选择集合
Random类中的一个方法:返回一个随机的整数
public int nextInt(int bound) :左闭右开,参数为范围的最大值+1
*/
public class RandomTest {
public static void main(String[] args) {
//1、创建随机类Random的对象
Random random = new Random();
//2、创建集合对象存储随机数
ArrayList<Integer> arr = new ArrayList<>();
//定义一个变量统计集合中是否有10个元素
int count = 0;
while (count<10){
//产生随机数
int i = random.nextInt(21);
//题目要求不能重复,要判断集合中是否有该随机数
if(!arr.contains(i)){
arr.add(i);//如果没有,就添加到集合里
count++;
}
}
System.out.println(arr);
}
}
执行结果如下:
[2, 17, 7, 5, 4, 1, 10, 19, 12, 14]
Process finished with exit code 0
3、键盘录入多个数据,以0结束,要求在控制台输出这多个数据中的最值
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class ArrayListTest1 {
public static void main(String[] args) {
//创建Scanner对象
Scanner sc = new Scanner(System.in);
//创建集合存储输入的数据
ArrayList<Integer> arr = new ArrayList<>();
while (true){
int number = sc.nextInt();//获取元素,sc.nextInt().var
if(number==0){ //如果键盘输入的是0,结束
break;
}else {
arr.add(number);
}
}
//Arrays工具类中有一个排序方法sort()
//集合转数组
Object[] objects = arr.toArray();
Arrays.sort(objects);
//排序后最后一个是最大值,第一个是最小值
//获取最大值
Object object = objects[objects.length - 1];
//获取最小值
Object object1 = objects[0];
//接收的是object类型,需要强转
Integer maxNumber = (Integer)objects[objects.length - 1];
Integer minNumber = (Integer)objects[0];
System.out.println("最大值为:"+maxNumber);
System.out.println("最小值为:"+minNumber);
}
}
执行结果如下:
随机输入数字,只有在最后输入0的时候,才能结束程序
12
23
345
567
6768
1122
8887
888
0
最大值为:8887
最小值为:12
Process finished with exit code 0