[实验任务一]:JAVA和C++常见数据结构迭代器的使用
信1305班共44名同学,每名同学都有姓名,学号和年龄等属性,分别使用JAVA内置迭代器和C++中标准模板库(STL)实现对同学信息的遍历,要求按照学号从小到大和从大到小两种次序输出学生信息。
实验要求:
1. 搜集并掌握JAVA和C++中常见的数据结构和迭代器的使用方法,例如,vector, list, map和set等;
2. 提交源代码;java:
package 实验18;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Random;
public class Main {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < 44; i++) {
students.add(new Student("Student" + i, 1000 + random.nextInt(1000), 18 + random.nextInt(5)));
}
// 按照学号从小到大排序
Collections.sort(students, (s1, s2) -> s1.getId() - s2.getId());
System.out.println("按照学号从小到大输出学生信息:");
Iterator<Student> iterator = students.iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
System.out.println(student.getName() + " " + student.getId() + " " + student.getAge());
}
// 按照学号从大到小排序
Collections.sort(students, (s1, s2) -> s2.getId() - s1.getId());
System.out.println("按照学号从大到小输出学生信息:");
iterator = students.iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
System.out.println(student.getName() + " " + student.getId() + " " + student.getAge());
}
}
}
标签:11,20,iterator,students,getId,util,student,java,打卡 From: https://www.cnblogs.com/yunbianshangdadun/p/17843177.html