课程类:
package com.aiuiot.collection;
/**
* 课程类
* @author aiuiot
*
*/
public class Course {
public String id; //课程id属性
public String name; //课程name属性
//为方便使用,添加含参数的构造器
public Course(String id,String name) {
this.id = id;
this.name = name;
}
//添加无参的构造器
public Course() {
}
}
学生类:
package com.aiuiot.collection;
import java.util.HashSet;
import java.util.Set;
/**
* 学生类
* @author aiuiot
*
*/
public class Student {
public String id;
public String name;
// 把课程信息存放在Set类型的属性里
public Set courses;
public Student(String id,String name) {
this.id = id;
this.name = name;
//给courses属性初始化,通过new HashSet()初始化
this.courses = new HashSet();
}
}
ListTest类
package com.aiuiot.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* 被选课程类
* @author aiuiot
*
*/
public class ListTest {
/**
* 用于存放被选课程的List
*/
public List courseToSelect;
//构造方法,并初始化courseToSelect属性
public ListTest() {
this.courseToSelect = new ArrayList();
}
/**
* 用于往courseToSelect中添加课程
*/
public void testAdd(){
//创建一个课程对象,并通过调用add()方法,添加到被选课程List中
Course cr1 = new Course("1", "《数据结构》");
courseToSelect.add(cr1);
//打印输出,提示打印课程,通过get()方法将元素取出来
Course temp = (Course) courseToSelect.get(0);
System.out.println("添加了课程:"+temp.id+":"+temp.name);
Course cr2 = new Course("2", "《Java编程思想》");
courseToSelect.add(0, cr2);
Course temp2 = (Course)courseToSelect.get(0);
System.out.println("添加了课程:"+temp2.id+":"+temp2.name);
courseToSelect.add(cr1);
Course temp0 = (Course)courseToSelect.get(2);
System.out.println("添加了课程:"+temp0.id+":"+temp0.name);
//以下方法1会抛出数组下标越界异常
// Course cr3 = new Course("3", "《从你的全世界路过》");
// courseToSelect.add(,cr3);
Course[] course = {new Course("3", "《离散数学》"),new Course("4", "《汇编语言》")};
courseToSelect.addAll(Arrays.asList(course));
Course temp3 = (Course)courseToSelect.get(3);
Course temp4 = (Course)courseToSelect.get(4);
System.out.println("添加了两门课程:"+temp3.id+":"+temp3.name+";"+temp4.id+":"+temp4.name);
Course[] course2 = {new Course("5", "《史记》"),new Course("6", "《资治通鉴》")};
courseToSelect.addAll(2, Arrays.asList(course2));
Course temp5 = (Course)courseToSelect.get(2);
Course temp6 = (Course)courseToSelect.get(3);
System.out.println("添加了两门课程:"+temp5.id+":"+temp5.name+";"+temp6.id+":"+temp6.name);
}
/**
* 取得List的元素的方法,方式一:通过for循环遍历
*/
public void testGet() {
int size = courseToSelect.size();
System.out.println("有如下课程待选:");
for(int i = 0;i < size;i++) {
Course cr= (Course)courseToSelect.get(i);
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/**
* 方式二:通过迭代器遍历List
* @param args
*/
public void testIterator() {
//通过集合的迭代器(iteartor)方法,取得迭代器的实例
Iterator it = courseToSelect.iterator();
System.out.println("有如下课程待选:(迭代器)");
while(it.hasNext()) {
Course cr = (Course)it.next();
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/**
* 方式二:迭代器的第二种写法
* 通过for each方法访问集合元素
* @param args
*/
public void testForEach() {
System.out.println("有如下课程待选:(迭代器 for each)");
for(Object obj:courseToSelect) {
Course cr = (Course)obj;
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/**
* 修改List中的元素
* @param args
*/
public void testModify() {
courseToSelect.set(4, new Course("7", "《毛概》"));
}
/**
* 删除List中元素
* @param args
*/
public void testRemove() {
// Course cr = (Course)courseToSelect.get(4);
// System.out.println("我是课程:"+cr.id+":"+cr.name+"我即将被删除");
// System.out.println("即将删除4位置伤的课程");
// courseToSelect.remove(4);
Course[] course = {(Course) courseToSelect.get(4),(Course) courseToSelect.get(5)};
courseToSelect.removeAll(Arrays.asList(course));
System.out.println("成功删除课程!");
testForEach();
}
public static void main(String[] args) {
ListTest lt = new ListTest();
lt.testAdd();
lt.testGet();
lt.testIterator();
lt.testForEach();
lt.testModify();
lt.testForEach();
lt.testRemove();
}
}
package com.aiuiot.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 测试泛型
* @author aiuiot
*
*/
public class TestGeneric {
//带有泛型Course,的list类型属性
public List<Course> courses;
public TestGeneric() {
this.courses = new ArrayList<Course>();
}
/**
* 测试添加
* @param args
*/
public void testAdd() {
Course cr1 = new Course("1", "大学语文");
courses.add(cr1);
//泛型集合中不能添加泛型规定的类型及其子类型以外的对象,否则会报错
//courses.add("能否添加一些奇怪的东西呢?");
Course cr2 = new Course("2", "Java 基础");
courses.add(cr2);
}
/**
* 测试循环遍历
* @param args
*/
public void testForEach() {
for(Course cr:courses) {
System.out.println(cr.id+" "+cr.name);
}
}
/**
* 泛型集合可以添加泛型的子类型的对象实例
* @param args
*/
public void testChild() {
ChildCourse ccr = new ChildCourse();
ccr.id = "3";
ccr.name = "我是子类型的课程对象实例";
courses.add(ccr);
}
/**
*泛型不能使用基本类型
* @param args
*/
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();
}
}
网友评论