1.Student类
import java.util.HashSet;
import java.util.Set;
public class Student {
public String id;
public String name;
public Set courses;
public Student(String id,String name){
this.id=id;
this.name=name;
this.courses=new HashSet();
}
}
- Course类
/**
* 课程类
* @author Administrator
*
*/
public class Course {
public String id;
public String name;
public Course(String id, String name) {
this.id = id ;
this.name = name;
}
public Course() {
}
}
- ListTest类
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* 备选课程类
*/
public class ListTest {
/**
* 用于存放备选课程的List
*/
public List coursesToSelect;
public ListTest() {
this.coursesToSelect = new ArrayList();
}
/**
* 用于往coursesToSelect中添加备选课程
*/
public void testAdd() {
//创建一个课程对象,并通过调用add方法,添加到备选课程List中
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);
coursesToSelect.add(cr1);
Course temp0 = (Course) coursesToSelect.get(2);
System.out.println("添加了课程:" + temp.id + ":" + temp.name);
//以下方法会抛出数组下标越界异常
//Course cr3 = new Course("3", "test");
//coursesToSelect.add(4, 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);
}
/**
* 取得List中的元素的方法
*/
public void testGet() {
int size = coursesToSelect.size();
System.out.println("有如下课程待选:");
for(int i= 0 ; i < size; i++) {
Course cr = (Course) coursesToSelect.get(i);
System.out.println("课程:" + cr.id + ":" + cr.name);
}
}
/**
* 通过迭代器来遍历List
*/
public void testIterator() {
//通过集合的iterator方法,取得迭代器的实例
Iterator<Course> it = coursesToSelect.iterator();
System.out.println("有如下课程待选(通过迭代器访问):");
while(it.hasNext()) {
Course cr = it.next();
System.out.println("课程:" + cr.id + ":" + cr.name);
}
}
/**
* 通过for each方法访问集合元素
*/
public void testForEach() {
System.out.println("有如下课程待选(通过for each访问):");
for (Object obj : coursesToSelect) {
Course cr = (Course) obj;
System.out.println("课程:" + cr.id + ":" + cr.name);
}
}
/**
* 修改List中的元素
*/
public void testModify() {
coursesToSelect.set(4, new Course("7", "毛概"));
}
/**
* 删除List中的元素
*/
public void testRemove() {
// Course cr = (Course) coursesToSelect.get(4);
System.out.println("即将删除4位置和5位置上的课程!");
Course[] courses = {
(Course) coursesToSelect.get(4), (Course) coursesToSelect.get(5)};
coursesToSelect.removeAll(Arrays.asList(courses));
// coursesToSelect.remove(4);
System.out.println("成功删除课程!");
testForEach();
}
/**
* 往List中添加一些奇怪的东西
*/
public void testType() {
System.out.println("能否往List中添加一些奇怪的东西呢!?");
coursesToSelect.add("我不是课程,我只是一个无辜的字符串!!");
}
public static void main( String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
lt.testType();
lt.testForEach();
// lt.testGet();
// lt.testIterator();
// lt.testForEach();
// lt.testModify();
// lt.testForEach();
// lt.testRemove();
}
}
/*输出:
添加了课程:1:数据结构
添加了课程:2:C语言
添加了课程:1:数据结构
添加了两门课程:1:数据结构;3:离散数学
添加了两门课程:5:高等数学;6:大学英语
能否往List中添加一些奇怪的东西呢!?
有如下课程待选(通过for each访问):
课程:2:C语言
课程:1:数据结构
课程:5:高等数学
课程:6:大学英语
课程:1:数据结构
课程:3:离散数学
课程:4:汇编语言
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to Course
at ListTest.testForEach(ListTest.java:94)
at ListTest.main(ListTest.java:131)
Process finished with exit code 1
*/
- SetTest类
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>();
}
/**
* 用于往coursesToSelect中添加备选课程
*/
public void testAdd() {
// 创建一个课程对象,并通过调用add方法,添加到备选课程List中
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);
// coursesToSelect.add(cr1);
// Course temp0 = (Course) coursesToSelect.get(2);
// System.out.println("添加了课程:" + temp.id + ":" + temp.name);
// 以下方法会抛出数组下标越界异常
// Course cr3 = new Course("3", "test");
// coursesToSelect.add(4, 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);
}
/**
* 通过for each方法访问集合元素
*
*/
public void testForEach() {
System.out.println("有如下课程待选(通过for each访问):");
for (Object obj : coursesToSelect) {
Course cr = (Course) obj;
System.out.println("课程:" + cr.id + ":" + cr.name);
}
}
/**
* @param args
*/
public static void main(String[] args) {
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中,添加某个对象,无论添加多少次, 最终只会保留一个该对象(的引用), 并且,保留的是第一次添加的那一个
*/
// student.courses.add(null);
student.courses.add(cr);
}
}
}
st.testForEachForSet(student);
}
public void testForEachForSet(Student student) {
// 打印输出,学生所选的课程!
System.out.println("共选择了:" + student.courses.size() + "门课程!");
for(Object cr:student.courses) {
}
}
}
/*输出:
"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" "-javaagent:D:\Shuang\IntelliJ IDEA\IntelliJ IDEA Community Edition 2020.3.1\lib\idea_rt.jar=55131:D:\Shuang\IntelliJ IDEA\IntelliJ IDEA Community Edition 2020.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;D:\code\untitled\out\production\untitled" SetTest
有如下课程待选(通过for each访问):
课程:2:C语言
课程:1:数据结构
课程:5:高等数学
课程:6:大学英语
课程:3:离散数学
课程:4:汇编语言
欢迎学生:小明选课!
请输入课程ID
1
请输入课程ID
2
请输入课程ID
3
共选择了:3门课程!
Process finished with exit code 0
*/
- TestGeneric类
import java.util.ArrayList;
import java.util.List;
public class TestGeneric {
/**
* 带有泛型——Course,的List类型属性
*/
public List<Course> courses;
public TestGeneric() {
this.courses = new ArrayList<Course>();
}
/**
* 测试添加
*/
public void testAdd() {
Course cr1 = new Course("1","大学语文");
courses.add(cr1);
//泛型集合中,不能添加泛型规定的类型及其子类型以外的对象,否则会报错!
// courses.add("能否添加一些奇怪的东西呢??");
Course cr2 = new Course("2","Java基础");
courses.add(cr2);
}
/**
* 测试循环遍历
*/
public void testForEach() {
for (Course cr : courses) {
System.out.println(cr.id + ":" + cr.name);
}
}
/**
* 泛型结合可以添加泛型的子类型的对象实例
*/
public void testChild() {
ChildCourse ccr = new ChildCourse();
ccr.id = "3";
ccr.name = "我是子类型的课程对象实例~~";
courses.add(ccr);
}
/**
* 泛型不能使用基本类型
*/
public void testBasicType() {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
System.out.println("基本类型必须使用包装类作为泛型!" + list.get(0));
}
public static void main(String[] args) {
TestGeneric tg = new TestGeneric();
tg.testAdd();
tg.testForEach();
tg.testChild();
tg.testForEach();
tg.testBasicType();
}
}
/*输出:
1:大学语文
2:Java基础
1:大学语文
2:Java基础
3:我是子类型的课程对象实例~~
基本类型必须使用包装类作为泛型!1
Process finished with exit code 0
*/
- ChildCourse类
public class ChildCourse extends Course {
}