标签:ram generate 使用 equals remove get arraylist ack ges
集合的概念:
Java中的集合类:是一种工具类,就像是容器,存储任意数量的具有共同属性的对象。
集合的作用:
1.在类的内部,对数据进行组织;
2.简单的快速的搜索大数据量的条目;
3.有的集合接口,提供了一系列排列有序的元素,并且 可以在序列中间快速的插入或删除有关的元素。
4.有的集合接口,提供了映射关系,可以通过 关键字(key)去快速查找到对应的唯一对象,而这个关键字可以是任意类型。
与数组的对比一为何选择集合而不是数组
1.数组的长度固定,集合长度可变
2.数组只能通过下标访问元素,类型固定,而有的集合可以通过任意类型查找所影射的具体对象
Java集合体系结构
Collection家族和Map家族组成。
Collection接口子接口以及实现类
Collection接口
1.是List、Set和Queue接口的父接口
2.定义了可用于操作List、Set和Queue的方法一增删改查
List接口及其实现类——ArrayList
1.List是元素有序并且可以重复的集合,被称为序列
2.List可以精确的控制每个元素的插入位置,或删除某个位置元素
3.ArrayList——数组序列,是List的一个重要的实现类
4.ArryList底层是由数组实现的
1.创建课程类(Course)
2.创建学生类
package com.imooc.collection; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; /* *备选课程类 * */ public class ListTest { /** * 用于存放备选课程List * */ public List CourseToSelect; public ListTest(){ this.CourseToSelect=new ArrayList(); } //用于往CourseToSelect中添加备选课程 public void addCourse(){ //创建一个课程对象,并通过调用add方法,添加到备选课程List中 Course c=new Course("1","数据结构"); CourseToSelect.add(c); Course temp=(Course) CourseToSelect.get(0); System.out.println("添加了一门课程"+temp.id+":"+temp.name); Course c1=new Course("2","C语言"); CourseToSelect.add(0, c1); Course temp1=(Course) CourseToSelect.get(0); System.out.println("添加一门课程:"+temp1.id+":"+temp1.name); Course c2=new Course("2","Test1"); CourseToSelect.add(4, c2); } public static void main(String []args){ ListTest list=new ListTest(); list.addCourse(); } }
运行结果为:
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/*
*备选课程类
* */
public class ListTest {
/**
* 用于存放备选课程List
* */
public List CourseToSelect;
public ListTest(){
this.CourseToSelect=new ArrayList();
}
//用于往CourseToSelect中添加备选课程
public void addCourse(){
//创建一个课程对象,并通过调用add方法,添加到备选课程List中
Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
CourseToSelect.addAll(Arrays.asList(course));
Course temp2=(Course) CourseToSelect.get(2);
Course temp3=(Course) CourseToSelect.get(3);
System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
CourseToSelect.addAll(2, Arrays.asList(course2));
Course temp4=(Course) CourseToSelect.get(2);
Course temp5=(Course) CourseToSelect.get(3);
System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
}
public static void main(String []args){
ListTest list=new ListTest();
list.addCourse();
}
}
运行结果为:
取得List中的元素的方法
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/*
*备选课程类
* */
public class ListTest {
/**
* 用于存放备选课程List
* */
public List CourseToSelect;
public ListTest(){
this.CourseToSelect=new ArrayList();
}
//用于往CourseToSelect中添加备选课程
public void addCourse(){
//创建一个课程对象,并通过调用add方法,添加到备选课程List中
Course c=new Course("1","数据结构");
CourseToSelect.add(c);
Course temp=(Course) CourseToSelect.get(0);
System.out.println("添加了一门课程"+temp.id+":"+temp.name);
Course c1=new Course("2","C语言");
CourseToSelect.add(0, c1);
Course temp1=(Course) CourseToSelect.get(0);
System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
Course cq=new Course("1","数据结构");
CourseToSelect.add(cq);
Course temp0=(Course) CourseToSelect.get(2);
System.out.println("添加了一门课程"+temp0.id+":"+temp0.name);
/*Course c2=new Course("2","Test1");
CourseToSelect.add(4, c2);*/
Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
CourseToSelect.addAll(Arrays.asList(course));
Course temp2=(Course) CourseToSelect.get(2);
Course temp3=(Course) CourseToSelect.get(3);
System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
CourseToSelect.addAll(2, Arrays.asList(course2));
Course temp4=(Course) CourseToSelect.get(2);
Course temp5=(Course) CourseToSelect.get(3);
System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
}
/*
* 取得List中的元素的方法
* */
public void testGet(){
System.out.println("有如下课程待选:");
int size=CourseToSelect.size();
for (int i = 0; i < size; i++) {
Course cr=(Course) CourseToSelect.get(i);
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
public static void main(String []args){
ListTest list=new ListTest();
list.addCourse();
list.testGet();
}
}
运行结果为:
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/*
*备选课程类
* */
public class ListTest {
/**
* 用于存放备选课程List
* */
public List CourseToSelect;
public ListTest(){
this.CourseToSelect=new ArrayList();
}
//用于往CourseToSelect中添加备选课程
public void addCourse(){
//创建一个课程对象,并通过调用add方法,添加到备选课程List中
Course c=new Course("1","数据结构");
CourseToSelect.add(c);
Course temp=(Course) CourseToSelect.get(0);
System.out.println("添加了一门课程"+temp.id+":"+temp.name);
Course c1=new Course("2","C语言");
CourseToSelect.add(0, c1);
Course temp1=(Course) CourseToSelect.get(0);
System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
Course cq=new Course("1","数据结构");
CourseToSelect.add(cq);
Course temp0=(Course) CourseToSelect.get(2);
System.out.println("添加了一门课程"+temp0.id+":"+temp0.name);
/*Course c2=new Course("2","Test1");
CourseToSelect.add(4, c2);*/
Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
CourseToSelect.addAll(Arrays.asList(course));
Course temp2=(Course) CourseToSelect.get(2);
Course temp3=(Course) CourseToSelect.get(3);
System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
CourseToSelect.addAll(2, Arrays.asList(course2));
Course temp4=(Course) CourseToSelect.get(2);
Course temp5=(Course) CourseToSelect.get(3);
System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
}
/*
* 取得List中的元素的方法
* */
public void testGet(){
System.out.println("有如下课程待选:");
int size=CourseToSelect.size();
for (int i = 0; i < size; i++) {
Course cr=(Course) CourseToSelect.get(i);
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 通过迭代器来遍历List
* */
public void testIterator(){
//通过集合iterator方法,取得迭代器实例
Iterator it=CourseToSelect.iterator();
System.out.println("有如下课程待选(通过迭代器访问):");
while (it.hasNext()) {
Course cr = (Course) it.next();
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
public static void main(String []args){
ListTest list=new ListTest();
list.addCourse();
list.testGet();
list.testIterator();
}
}
运行结果为:
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/*
*备选课程类
* */
public class ListTest {
/**
* 用于存放备选课程List
* */
public List CourseToSelect;
public ListTest(){
this.CourseToSelect=new ArrayList();
}
//用于往CourseToSelect中添加备选课程
public void addCourse(){
//创建一个课程对象,并通过调用add方法,添加到备选课程List中
Course c=new Course("1","数据结构");
CourseToSelect.add(c);
Course temp=(Course) CourseToSelect.get(0);
System.out.println("添加了一门课程"+temp.id+":"+temp.name);
Course c1=new Course("2","C语言");
CourseToSelect.add(0, c1);
Course temp1=(Course) CourseToSelect.get(0);
System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
Course cq=new Course("1","数据结构");
CourseToSelect.add(cq);
Course temp0=(Course) CourseToSelect.get(2);
System.out.println("添加了一门课程"+temp0.id+":"+temp0.name);
/*Course c2=new Course("2","Test1");
CourseToSelect.add(4, c2);*/
Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
CourseToSelect.addAll(Arrays.asList(course));
Course temp2=(Course) CourseToSelect.get(2);
Course temp3=(Course) CourseToSelect.get(3);
System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
CourseToSelect.addAll(2, Arrays.asList(course2));
Course temp4=(Course) CourseToSelect.get(2);
Course temp5=(Course) CourseToSelect.get(3);
System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
}
/*
* 取得List中的元素的方法
* */
public void testGet(){
System.out.println("有如下课程待选:");
int size=CourseToSelect.size();
for (int i = 0; i < size; i++) {
Course cr=(Course) CourseToSelect.get(i);
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 通过迭代器来遍历List
* */
public void testIterator(){
//通过集合iterator方法,取得迭代器实例
Iterator it=CourseToSelect.iterator();
System.out.println("有如下课程待选(通过迭代器访问):");
while (it.hasNext()) {
Course cr = (Course) it.next();
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 通过for each方法访问集合元素
*
* */
public void testForEach(){
System.out.println("有如下课程待选(通过 for each访问):");
for (Object object : CourseToSelect) {
Course cr=(Course) object;
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
public static void main(String []args){
ListTest list=new ListTest();
list.addCourse();
list.testGet();
list.testIterator();
list.testForEach();
}
}
运行结果为:
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/*
*备选课程类
* */
public class ListTest {
/**
* 用于存放备选课程List
* */
public List CourseToSelect;
public ListTest(){
this.CourseToSelect=new ArrayList();
}
//用于往CourseToSelect中添加备选课程
public void addCourse(){
//创建一个课程对象,并通过调用add方法,添加到备选课程List中
Course c=new Course("1","数据结构");
CourseToSelect.add(c);
Course temp=(Course) CourseToSelect.get(0);
System.out.println("添加了一门课程"+temp.id+":"+temp.name);
Course c1=new Course("2","C语言");
CourseToSelect.add(0, c1);
Course temp1=(Course) CourseToSelect.get(0);
System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
Course cq=new Course("1","数据结构");
CourseToSelect.add(cq);
Course temp0=(Course) CourseToSelect.get(2);
System.out.println("添加了一门课程"+temp0.id+":"+temp0.name);
/*Course c2=new Course("2","Test1");
CourseToSelect.add(4, c2);*/
Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
CourseToSelect.addAll(Arrays.asList(course));
Course temp2=(Course) CourseToSelect.get(2);
Course temp3=(Course) CourseToSelect.get(3);
System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
CourseToSelect.addAll(2, Arrays.asList(course2));
Course temp4=(Course) CourseToSelect.get(2);
Course temp5=(Course) CourseToSelect.get(3);
System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
}
/*
* 取得List中的元素的方法
* */
public void testGet(){
System.out.println("有如下课程待选:");
int size=CourseToSelect.size();
for (int i = 0; i < size; i++) {
Course cr=(Course) CourseToSelect.get(i);
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 通过迭代器来遍历List
* */
public void testIterator(){
//通过集合iterator方法,取得迭代器实例
Iterator it=CourseToSelect.iterator();
System.out.println("有如下课程待选(通过迭代器访问):");
while (it.hasNext()) {
Course cr = (Course) it.next();
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 通过for each方法访问集合元素
*
* */
public void testForEach(){
System.out.println("有如下课程待选(通过 for each访问):");
for (Object object : CourseToSelect) {
Course cr=(Course) object;
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 修改List中的元素
* */
public void testModify(){
CourseToSelect.set(4,new Course("7","毛概"));
}
public static void main(String []args){
ListTest list=new ListTest();
list.addCourse();
list.testGet();
list.testIterator();
list.testForEach();
list.testModify();
list.testForEach();
}
}
运行结果为:
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/*
*备选课程类
* */
public class ListTest {
/**
* 用于存放备选课程List
* */
public List CourseToSelect;
public ListTest(){
this.CourseToSelect=new ArrayList();
}
//用于往CourseToSelect中添加备选课程
public void addCourse(){
//创建一个课程对象,并通过调用add方法,添加到备选课程List中
Course c=new Course("1","数据结构");
CourseToSelect.add(c);
Course temp=(Course) CourseToSelect.get(0);
System.out.println("添加了一门课程"+temp.id+":"+temp.name);
Course c1=new Course("2","C语言");
CourseToSelect.add(0, c1);
Course temp1=(Course) CourseToSelect.get(0);
System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
Course cq=new Course("1","数据结构");
CourseToSelect.add(cq);
Course temp0=(Course) CourseToSelect.get(2);
System.out.println("添加了一门课程"+temp0.id+":"+temp0.name);
/*Course c2=new Course("2","Test1");
CourseToSelect.add(4, c2);*/
Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
CourseToSelect.addAll(Arrays.asList(course));
Course temp2=(Course) CourseToSelect.get(2);
Course temp3=(Course) CourseToSelect.get(3);
System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
CourseToSelect.addAll(2, Arrays.asList(course2));
Course temp4=(Course) CourseToSelect.get(2);
Course temp5=(Course) CourseToSelect.get(3);
System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
}
/*
* 取得List中的元素的方法
* */
public void testGet(){
System.out.println("有如下课程待选:");
int size=CourseToSelect.size();
for (int i = 0; i < size; i++) {
Course cr=(Course) CourseToSelect.get(i);
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 通过迭代器来遍历List
* */
public void testIterator(){
//通过集合iterator方法,取得迭代器实例
Iterator it=CourseToSelect.iterator();
System.out.println("有如下课程待选(通过迭代器访问):");
while (it.hasNext()) {
Course cr = (Course) it.next();
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 通过for each方法访问集合元素
*
* */
public void testForEach(){
System.out.println("有如下课程待选(通过 for each访问):");
for (Object object : CourseToSelect) {
Course cr=(Course) object;
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 修改List中的元素
* */
public void testModify(){
CourseToSelect.set(4,new Course("7","毛概"));
}
/*
* 删除list中的元素
* */
public void testRemove(){
Course cr=(Course) CourseToSelect.get(4);
System.out.println("我是课程:"+cr.id+":"+cr.name+"我即将被要删除");
CourseToSelect.remove(cr);
System.out.println("成功删除课程");
testForEach();
}
public static void main(String []args){
ListTest list=new ListTest();
list.addCourse();
list.testGet();
list.testIterator();
list.testForEach();
list.testModify();
list.testForEach();
list.testRemove();
}
}
运行结果为:
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/*
*备选课程类
* */
public class ListTest {
/**
* 用于存放备选课程List
* */
public List CourseToSelect;
public ListTest(){
this.CourseToSelect=new ArrayList();
}
//用于往CourseToSelect中添加备选课程
public void addCourse(){
//创建一个课程对象,并通过调用add方法,添加到备选课程List中
Course c=new Course("1","数据结构");
CourseToSelect.add(c);
Course temp=(Course) CourseToSelect.get(0);
System.out.println("添加了一门课程"+temp.id+":"+temp.name);
Course c1=new Course("2","C语言");
CourseToSelect.add(0, c1);
Course temp1=(Course) CourseToSelect.get(0);
System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
Course cq=new Course("1","数据结构");
CourseToSelect.add(cq);
Course temp0=(Course) CourseToSelect.get(2);
System.out.println("添加了一门课程"+temp0.id+":"+temp0.name);
/*Course c2=new Course("2","Test1");
CourseToSelect.add(4, c2);*/
Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
CourseToSelect.addAll(Arrays.asList(course));
Course temp2=(Course) CourseToSelect.get(2);
Course temp3=(Course) CourseToSelect.get(3);
System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
CourseToSelect.addAll(2, Arrays.asList(course2));
Course temp4=(Course) CourseToSelect.get(2);
Course temp5=(Course) CourseToSelect.get(3);
System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
}
/*
* 取得List中的元素的方法
* */
public void testGet(){
System.out.println("有如下课程待选:");
int size=CourseToSelect.size();
for (int i = 0; i < size; i++) {
Course cr=(Course) CourseToSelect.get(i);
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 通过迭代器来遍历List
* */
public void testIterator(){
//通过集合iterator方法,取得迭代器实例
Iterator it=CourseToSelect.iterator();
System.out.println("有如下课程待选(通过迭代器访问):");
while (it.hasNext()) {
Course cr = (Course) it.next();
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 通过for each方法访问集合元素
*
* */
public void testForEach(){
System.out.println("有如下课程待选(通过 for each访问):");
for (Object object : CourseToSelect) {
Course cr=(Course) object;
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 修改List中的元素
* */
public void testModify(){
CourseToSelect.set(4,new Course("7","毛概"));
}
/*
* 删除list中的多组元素
* */
public void testRemove(){
System.out.println("即将删除4位置和5位置上的课程");
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 list=new ListTest();
list.addCourse();
list.testGet();
list.testIterator();
list.testForEach();
list.testModify();
list.testForEach();
list.testRemove();
}
}
运行结果为:
泛型
集合中的元素,可以是任意类型的对象(对象的引用)。如果把某一个对象放入集合,则会忽略他的类型,而把他当做Object处理
泛型则是规定了某个集合只可以存放特定类型的对象。会在编译期间进行类型检查,可以直接按指定类型获取集合元素
package com.imooc.collection;
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);
Course cr2=new Course("2","Java基础");
courses.add(cr2);
//泛型集合中,不能添加泛型规定的类型以外的对象,否则会报错!
//courses.add("可以添加奇怪的东西吗??");
}
//测试循环遍历
public void testForEach(){
for (Course cr:courses) {
System.out.println(cr.id+":"+cr.name);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestGeneric tg=new TestGeneric();
tg.testAdd();
tg.testForEach();
}
}
1.泛型集合中的限定类型不能使用基本数据类型。
2.可以通过使用包装类限定允许存入的基本数据类型。
Set接口及其实现类——HashSet
1.set是元素无序并且不可以重复的集合,被称为集。
2.HashSet——哈希集,是Set的一个重要实现类
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class SetTest {
public List<Course>coursesToSelect;
public SetTest(){
coursesToSelect=new ArrayList<Course>();
}
//用于往CourseToSelect中添加备选课程
public void testAdd(){
//创建一个课程对象,并通过调用add方法,添加到备选课程List中
Course c=new Course("1","数据结构");
coursesToSelect.add(c);
Course temp=(Course) coursesToSelect.get(0);
System.out.println("添加了一门课程"+temp.id+":"+temp.name);
Course c1=new Course("2","C语言");
coursesToSelect.add(0, c1);
Course temp1=(Course) coursesToSelect.get(0);
System.out.println("添加一门课程:"+temp1.id+":"+temp1.name);
Course cq=new Course("1","数据结构");
coursesToSelect.add(cq);
Course temp0=(Course) coursesToSelect.get(2);
System.out.println("添加了一门课程"+temp0.id+":"+temp0.name);
/*Course c2=new Course("2","Test1");
CourseToSelect.add(4, c2);*/
Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
coursesToSelect.addAll(Arrays.asList(course));
Course temp2=(Course) coursesToSelect.get(2);
Course temp3=(Course) coursesToSelect.get(3);
System.out.println("添加了两门课程:"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
Course[] course2={new Course("5","高等数学"),new Course("6","数字逻辑")};
coursesToSelect.addAll(2, Arrays.asList(course2));
Course temp4=(Course) coursesToSelect.get(2);
Course temp5=(Course) coursesToSelect.get(3);
System.out.println("添加了两门课程:"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
}
/* * 取得List中的元素的方法
* */
public void testGet(){
System.out.println("有如下课程待选:");
int size=coursesToSelect.size();
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 it=coursesToSelect.iterator();
System.out.println("有如下课程待选(通过迭代器访问):");
while (it.hasNext()) {
Course cr = (Course) it.next();
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/** 通过for each方法访问集合元素
*
* */
public void testForEach(){
System.out.println("有如下课程待选(通过 for each访问):");
for (Object object : coursesToSelect) {
Course cr=(Course) object;
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/**
* @param argsS
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SetTest st=new SetTest();
st.testAdd();
st.testForEach();
//创建一个学生对象
Student student=new Student("1","小明"