goodY
2019-08-08 15:20
照着老师视频里写的,功能也都实现了,但是并没有每次运行程序输出顺序都不同,而是下图这样每次都是142
代码为
package com.imooc.collection;import java.util.Arrays;import java.util.Iterator;import java.util.List;import java.util.ArrayList;import java.util.Scanner;public class SetTest { public List<Course> coursesToSelect; public SetTest() { this.coursesToSelect = new ArrayList<Course>(); } public void testAdd() { // 创建一个课程对象,并通过调用add方法, // 添加到备选课程Lis中 Course cr1 = new Course("1", "数据结构"); coursesToSelect.add(cr1); Course temp = (Course) coursesToSelect.get(0); System.out.println("添加了课程:" + temp.id + " " + temp.name); Course cr2 = new Course("2", "C语言"); coursesToSelect.add(0, cr2); Course temp2 = (Course) coursesToSelect.get(0);// System.out.println("添加了课程:" + temp2.id + " " + temp2.name); // 以下两行代码会抛出数组越界异常// Course cr3 = new Course("3","test");// coursesToSelect.add(3,cr3); Course[] course = { new Course("3", "离散数学"), new Course("4", "汇编语言") }; coursesToSelect.addAll(Arrays.asList(course)); Course temp3 = (Course) coursesToSelect.get(2); Course temp4 = (Course) coursesToSelect.get(3);// System.out.println("添加了两门课程:" + temp3.id + ":" + temp3.name// + ";" + temp4.id + ":" + temp4.name); Course[] course2 = { new Course("5", "高等数学"), new Course("6", "大学英语") }; coursesToSelect.addAll(2, Arrays.asList(course2)); Course temp5 = (Course) coursesToSelect.get(2); Course temp6 = (Course) coursesToSelect.get(3);// System.out.println("添加了两门课程:" + temp5.id + ":" + temp5.name// + ";" + temp6.id + ":" + temp6.name);// coursesToSelect.add(new Course("1","数据结构")); } /** * 通过迭代器来遍历List * * @param args */ public void testIterator() { // 通过集合的iterator方法,取得迭代器的实例 Iterator it = coursesToSelect.iterator(); System.out.println("有如下课程待选(通过迭代器访问)"); while (it.hasNext()) { Course cr = (Course) it.next(); System.out.println("课程" + cr.id + ":" + cr.name); } } /** * 通过foreach来遍历List * * @param args */ public void testForEach() { System.out.println("有如下课程待选(通过foreach访问)"); for (Object obj : coursesToSelect) { Course cr = (Course) obj; System.out.println("课程" + cr.id + ":" + cr.name); } } public static void main(String[] args) { // TODO Auto-generated method stub SetTest st = new SetTest(); st.testAdd(); st.testForEach(); //创建一个学生对象 Student student = new Student("1","小明"); System.out.println("欢迎学生 " + student.name + "选课"); //创建一个Scanner类对象,用了接受从键盘输入的课程ID Scanner console = new Scanner(System.in); for (int i = 0;i < 3;i++) { System.out.println("请输入课程ID"); String courseId = console.next(); for (Course cr : st.coursesToSelect) { if (cr.id.equals(courseId)) { student.courses.add(cr); /** * Set中,添加某个对象,无论添加多少次 * 最终都只会保留一个该对象(的引用) * 并且,保留的是第一次添加的那一个 * 还可以像以下这样添加null,但实际应用没什么意义 * student.courses.add(null); */ student.courses.add(cr); } } } st.testForEachForSet(student); } public void testForEachForSet(Student student) { //打印输出,学生所选的课程 System.out.println("共选择了:" + student.courses.size() + "门课程"); for (Course cr : student.courses) { System.out.println("选择了课程:" + cr.id + ":" + cr.name); } }}
java的jdk新的版本对原有的一些方法进行了改动。现在是一样的
同问,我输入123,输出都213不变的,什么情况
set集合是无序集合 没有固定顺序
Set 是无序的,在输入后记录是无序,所以输出是也是无序的
Java入门第三季
409792 学习 · 4340 问题
相似问题