coursr.java
package com.imooc.collection;
/*
* 课程类
*/
public class course {
public String id;
public String name;
public course(String id,String name){
this.id=id;
this.name=name;
}
}
student.java
package com.imooc.collection;
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();
}
}
删除方法1:把要删除的值先取出来传给remove,直接移除
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<course> coursesToSelect;
public ListTest (){
this.coursesToSelect = new ArrayList<course>();
}
/*
* 用于往courseToSelect中添加备选课程
*/
public void TestAdd(){
course cr1 = new course("1","数据结构");
coursesToSelect.add(cr1);
course temp1 = (course)coursesToSelect.get(0);
System.out.println("添加了课程:"+temp1.id+temp1.name);
course cr2 = new course("2","C语言");
coursesToSelect.add(1,cr2);
course temp2 = (course)coursesToSelect.get(1);
System.out.println("添加了课程:"+temp2.id+temp2.name);
coursesToSelect.add(cr1);
course temp0 = (course)coursesToSelect.get(2);
System.out.println("添加了课程:"+temp0.id+temp0.name);
course cr3 = new course("3","Java语言");
coursesToSelect.add(2,cr3);
course temp3 = (course)coursesToSelect.get(2);
System.out.println("添加了课程:"+temp3.id+temp3.name);
course[] cour1 = {new course("4","离散数学"),new course("5","汇编语言")};
coursesToSelect.addAll(3,Arrays.asList(cour1));
course temp4 = (course)coursesToSelect.get(3);
course temp5 = (course)coursesToSelect.get(4);
System.out.println("添加了两门课程"+temp4.id+temp4.name+temp5.id+temp5.name);
course[] cour2 = {new course("6","高等数学"),new course("7","线性代数")};
coursesToSelect.addAll(5,Arrays.asList(cour2));
course temp6 = (course)coursesToSelect.get(5);
course temp7 = (course)coursesToSelect.get(6);
System.out.println("添加了两门课程"+temp6.id+temp6.name+temp7.id+temp7.name);
course[] cour3 = {new course("8","高等数学"),new course("9","线性代数")};
coursesToSelect.addAll(7,Arrays.asList(cour3));
course temp8 = (course)coursesToSelect.get(7);
course temp9 = (course)coursesToSelect.get(8);
System.out.println("添加了两门课程"+temp8.id+temp8.name+temp9.id+temp9.name);
}
/*
* 通过for循环取得List中的元素的方法
*
*/
public void testGet(){
int size = coursesToSelect.size();
System.out.println("有如下课程待选(for访问)");
for(int i = 0 ; i < size ; i++){
course cr = (course)coursesToSelect.get(i);
System.out.println(cr.id+cr.name);
}
}
/*
* 通过iterator取得List中的元素的方法
* 迭代器依赖集合存在
*/
public void testIterator(){
Iterator it = coursesToSelect.iterator();
System.out.println("有如下课程待选(迭代器访问)");
while(it.hasNext()){
course cr = (course)it.next();
System.out.println(cr.id+cr.name);
}
}
/*
* 通过foreach取得List中的元素的方法
*
*/
public void testForEach(){
System.out.println("有如下课程待选(foreach访问)");
for(Object obj : coursesToSelect){//把集合中每个元素取出来作为一个object用
course cr = (course)obj;
System.out.println(cr.id+cr.name);
}
}
/*
* 修改List中的元素
*/
public void testModify(){
coursesToSelect.set(4,new course("7","毛概"));
testForEach();
}
/*
* 删除List中的元素
*
*/
public void testRemove(){
course cr = (course)coursesToSelect.get(4);
System.out.println("课程"+cr.id+cr.name+"即将被删除");
coursesToSelect.remove(cr);
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.testRemove();
}
}
删除方法2:
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<course> coursesToSelect;
public ListTest (){
this.coursesToSelect = new ArrayList<course>();
}
/*
* 用于往courseToSelect中添加备选课程
*/
public void TestAdd(){
course cr1 = new course("1","数据结构");
coursesToSelect.add(cr1);
course temp1 = (course)coursesToSelect.get(0);
System.out.println("添加了课程:"+temp1.id+temp1.name);
course cr2 = new course("2","C语言");
coursesToSelect.add(1,cr2);
course temp2 = (course)coursesToSelect.get(1);
System.out.println("添加了课程:"+temp2.id+temp2.name);
coursesToSelect.add(cr1);
course temp0 = (course)coursesToSelect.get(2);
System.out.println("添加了课程:"+temp0.id+temp0.name);
course cr3 = new course("3","Java语言");
coursesToSelect.add(2,cr3);
course temp3 = (course)coursesToSelect.get(2);
System.out.println("添加了课程:"+temp3.id+temp3.name);
course[] cour1 = {new course("4","离散数学"),new course("5","汇编语言")};
coursesToSelect.addAll(3,Arrays.asList(cour1));
course temp4 = (course)coursesToSelect.get(3);
course temp5 = (course)coursesToSelect.get(4);
System.out.println("添加了两门课程"+temp4.id+temp4.name+temp5.id+temp5.name);
course[] cour2 = {new course("6","高等数学"),new course("7","线性代数")};
coursesToSelect.addAll(5,Arrays.asList(cour2));
course temp6 = (course)coursesToSelect.get(5);
course temp7 = (course)coursesToSelect.get(6);
System.out.println("添加了两门课程"+temp6.id+temp6.name+temp7.id+temp7.name);
course[] cour3 = {new course("8","高等数学"),new course("9","线性代数")};
coursesToSelect.addAll(7,Arrays.asList(cour3));
course temp8 = (course)coursesToSelect.get(7);
course temp9 = (course)coursesToSelect.get(8);
System.out.println("添加了两门课程"+temp8.id+temp8.name+temp9.id+temp9.name);
}
/*
* 通过for循环取得List中的元素的方法
*
*/
public void testGet(){
int size = coursesToSelect.size();
System.out.println("有如下课程待选(for访问)");
for(int i = 0 ; i < size ; i++){
course cr = (course)coursesToSelect.get(i);
System.out.println(cr.id+cr.name);
}
}
/*
* 通过iterator取得List中的元素的方法
* 迭代器依赖集合存在
*/
public void testIterator(){
Iterator it = coursesToSelect.iterator();
System.out.println("有如下课程待选(迭代器访问)");
while(it.hasNext()){
course cr = (course)it.next();
System.out.println(cr.id+cr.name);
}
}
/*
* 通过foreach取得List中的元素的方法
*
*/
public void testForEach(){
System.out.println("有如下课程待选(foreach访问)");
for(Object obj : coursesToSelect){//把集合中每个元素取出来作为一个object用
course cr = (course)obj;
System.out.println(cr.id+cr.name);
}
}
/*
* 修改List中的元素
*/
public void testModify(){
coursesToSelect.set(4,new course("7","毛概"));
testForEach();
}
/*
* 删除List中的元素
*
*/
public void testRemove(){
coursesToSelect.remove(4);
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.testRemove();
}
}
这个方法也是一样的效果,就是省去了取出来的过程
删除方法3:
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<course> coursesToSelect;
public ListTest (){
this.coursesToSelect = new ArrayList<course>();
}
/*
* 用于往courseToSelect中添加备选课程
*/
public void TestAdd(){
course cr1 = new course("1","数据结构");
coursesToSelect.add(cr1);
course temp1 = (course)coursesToSelect.get(0);
System.out.println("添加了课程:"+temp1.id+temp1.name);
course cr2 = new course("2","C语言");
coursesToSelect.add(1,cr2);
course temp2 = (course)coursesToSelect.get(1);
System.out.println("添加了课程:"+temp2.id+temp2.name);
coursesToSelect.add(cr1);
course temp0 = (course)coursesToSelect.get(2);
System.out.println("添加了课程:"+temp0.id+temp0.name);
course cr3 = new course("3","Java语言");
coursesToSelect.add(2,cr3);
course temp3 = (course)coursesToSelect.get(2);
System.out.println("添加了课程:"+temp3.id+temp3.name);
course[] cour1 = {new course("4","离散数学"),new course("5","汇编语言")};
coursesToSelect.addAll(3,Arrays.asList(cour1));
course temp4 = (course)coursesToSelect.get(3);
course temp5 = (course)coursesToSelect.get(4);
System.out.println("添加了两门课程"+temp4.id+temp4.name+temp5.id+temp5.name);
course[] cour2 = {new course("6","高等数学"),new course("7","线性代数")};
coursesToSelect.addAll(5,Arrays.asList(cour2));
course temp6 = (course)coursesToSelect.get(5);
course temp7 = (course)coursesToSelect.get(6);
System.out.println("添加了两门课程"+temp6.id+temp6.name+temp7.id+temp7.name);
course[] cour3 = {new course("8","高等数学"),new course("9","线性代数")};
coursesToSelect.addAll(7,Arrays.asList(cour3));
course temp8 = (course)coursesToSelect.get(7);
course temp9 = (course)coursesToSelect.get(8);
System.out.println("添加了两门课程"+temp8.id+temp8.name+temp9.id+temp9.name);
}
/*
* 通过for循环取得List中的元素的方法
*
*/
public void testGet(){
int size = coursesToSelect.size();
System.out.println("有如下课程待选(for访问)");
for(int i = 0 ; i < size ; i++){
course cr = (course)coursesToSelect.get(i);
System.out.println(cr.id+cr.name);
}
}
/*
* 通过iterator取得List中的元素的方法
* 迭代器依赖集合存在
*/
public void testIterator(){
Iterator it = coursesToSelect.iterator();
System.out.println("有如下课程待选(迭代器访问)");
while(it.hasNext()){
course cr = (course)it.next();
System.out.println(cr.id+cr.name);
}
}
/*
* 通过foreach取得List中的元素的方法
*
*/
public void testForEach(){
System.out.println("有如下课程待选(foreach访问)");
for(Object obj : coursesToSelect){//把集合中每个元素取出来作为一个object用
course cr = (course)obj;
System.out.println(cr.id+cr.name);
}
}
/*
* 修改List中的元素
*/
public void testModify(){
coursesToSelect.set(4,new course("7","毛概"));
testForEach();
}
/*
* 删除List中的元素
*
*/
public void testRemove(){
course[] cr = {(course)coursesToSelect.get(4),(course)coursesToSelect.get(5)};
coursesToSelect.removeAll(Arrays.asList(cr));
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.testRemove();
}
}
从结果可以看到我们成功的删除了4,5,位置的课程,写到这里我又对Arrays.asList有了新的理解,其实我们的coursesToSelect()就是一个ArrayList,这两个的性质差不多addAll的时候是把新建的多个课程组成一个Arrays.asList和ArrayList连接起来,这个删除也是一样的原理