观小呆
2019-05-05 11:32
跟着老师打的代码,从ListTest.java复制过来的testAdd()方法后,除了注释system.println.out输出语句,我还注掉了第35行,不然运行后控制台报错,可是我看老师没有注释这一行!只注释了输出语句,为啥呀?这是第一个问题,还有第二个问题,就是我添加的课程,明明是先写的离散数学和汇编语言,后写的高等数学和大学英雄,可是为什么testForEach()方法遍历后,控制台的信息离散数学和汇编语言是排在高等数学和大学英语后面的呢?麻烦会的小伙伴讲一讲哦。谢谢
coursesToSelect.addAll(2, Arrays.asList(course2));下标你标的是2
你添加了4个,到那时这个数字和数组一样都是从0开始的,所以最大的才3
package imooc.imooc.collection;import java.util.HashSet;import java.util.Set;/** * 学生类 * */public class Student { private String id; private String name; public Set<Course> courses; public Student(String id, String name) { this.id = id; this.name = name; // Set接口,无序不重复 this.courses = new HashSet<Course>(); } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getCourses() { return courses; } public void setCourses(Set courses) { this.courses = courses; }}
package imooc.imooc.collection;/** * 课程类 * */public class Course { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Course(String id, String name) { this.id = id; this.name = name; } public Course() { }}
package imooc.imooc.collection;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Scanner;public class SetTest { public List<Course> coursesToSelect; public SetTest() { coursesToSelect = new ArrayList<Course>(); } public void testAdd() { // 创建一个课程对象,并通过调用add方法,将课程对象添加到备选课程List中 Course cr1 = new Course("1", "数据结构"); coursesToSelect.add(cr1); Course cr2 = new Course("2", "C语言"); coursesToSelect.add(0, cr2); Course[] course = { new Course("3", "离散数学"), new Course("4", "汇编语言") }; coursesToSelect.addAll(Arrays.asList(course)); Course[] course2 = { new Course("5", "高等数学"), new Course("6", "大学英语") }; coursesToSelect.addAll(2, Arrays.asList(course2)); } public void testForeach() { for (Object obj : coursesToSelect) { Course cr = (Course) obj; System.out.println("课程:" + cr.getId() + ":" + cr.getName()); } } public void testForEachForSet(Student student) { System.out.println("共选择了" + student.courses.size() + "门课程"); // 打印输出,学生所选的课程! for (Course cr : student.courses) { System.out.println("选了课程:" + cr.getId() + ":" + cr.getName()); } } 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.getName() + "选课!"); // 创建一个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.getId().equals(courseID)) { student.courses.add(cr); /** * Set中,添加某个对象,最终只保留一个该对象(的引用) Set中,可以添加null * Set中,可以添加null */ //student.courses.add(cr); } } } // 打印输出,学生所选的课程! st.testForEachForSet(student); }}
数组下标越界了,0~3,位置是0,1,2,3,一共四个,哪有4,只有到3
Java入门第三季
409792 学习 · 4340 问题
相似问题