Java集合中List的用法

List接口是Collection接口的子接口,List有一个重要的实现类--ArrayList类,List中的元素是有序排列的而且可重复,所以被称为是序列。

List可以精确的控制每个元素的插入位置,或删除某个位置元素,它的实现类ArrayList底层是由数组实现的。

List中有增删改查的方法,我们可以通过例子演示:

我们通过对学生选课,来演示List中对课程增删改查的方法

复制代码
 1 /**
 2  * 课程类
 3  * @author lenovo
 4  *
 5  */
 6 public class KeCheng {
 7 
 8     public String id;
 9     
10     public String name;
11     
12     public KeCheng(String id,String name){
13         this.id = id;
14         this.name = name;
15     }
16     
17 }
复制代码

创建一个学生类

 

复制代码
 1 import java.util.HashSet;
 2 import java.util.Set;
 3 
 4 /**
 5  * 学生类
 6  * @author lenovo
 7  *
 8  */
 9 public class Student {
10 
11     public String id;
12     
13     public String name;
14     
15     public Set kecheng;
16     
17     public Student(String id,String name){
18         this.id = id;
19         this.name = name;
20         this.kecheng = new HashSet();
21     }
22     
23 }
复制代码

 

创建一个备选课程的类

复制代码
 1 /**
 2  * 备选课程类
 3  * @author lenovo
 4  *
 5  */
 6 public class ListTest {
 7 
 8     //用于存放备选课程的List
 9     public List beixuan;
10     
11     public ListTest(){
12         this.beixuan = new ArrayList();
13     }
14 }
复制代码

 

可以使用List中add方法添加课程

复制代码
 1 /*
 2      * 用来往beixuan里添加备选课程的方法
 3      */
 4     public void kcAdd(){
 5         
 6         //创建一个课程的对象,调用add方法,添加到备选课程的List中
 7         KeCheng kc = new KeCheng("1","数据结构");
 8         beixuan.add(kc);
 9         
10         KeCheng kc2 = new KeCheng("2","C语言");
11         beixuan.add(0, kc2);
12         
13         KeCheng[] kcArr = {new KeCheng("3","大学语文"),new KeCheng("4","线性代数")};
14         beixuan.addAll(Arrays.asList(kcArr));
15         
16         KeCheng[] kcArr2 = {new KeCheng("5","艺术设计"),new KeCheng("6","计算机基础")};
17         beixuan.addAll(2, Arrays.asList(kcArr2));
18         
19     }
20     
复制代码

 

扫描二维码关注公众号,回复: 1784550 查看本文章

使用get方法取出指定位置上的内容,并用for循环遍历出课程的内容

复制代码
 1 /*
 2      * 使用for循环遍历课程的方法
 3      */
 4     public void getKC(){
 5         int size = beixuan.size();
 6         System.out.println("使用for遍历课程:");
 7         for (int i = 0; i < size; i++) {
 8             KeCheng kc = (KeCheng) beixuan.get(i);
 9             System.out.println("课程:"+kc.id+":"+kc.name+";");
10         }
11     }
复制代码

 

遍历List的内容,除了使用for循环外,还可以使用迭代器、foreach等方法

复制代码
 1 /*
 2      * 使用Iterator迭代器遍历课程
 3      */
 4     public void testIterator(){
 5         Iterator it = beixuan.iterator();
 6         System.out.println("使用Iterator遍历课程:");
 7         while(it.hasNext()){
 8             KeCheng kc = (KeCheng) it.next();
 9             System.out.println("课程:"+kc.id+":"+kc.name+";");
10         }
11         
12     }
13     
14     /*
15      * 使用for each遍历课程
16      */
17     public void testForeach(){
18         System.out.println("使用for each遍历课程:");
19         for(Object obj : beixuan){
20             KeCheng kc = (KeCheng)obj;
21             System.out.println("课程:"+kc.id+":"+kc.name+";");
22         }
23     }
24     
复制代码

 

修改List中的元素,可以使用set方法

1 /*
2      * 使用set方法添加课程
3      */
4     public void testModify(){
5         beixuan.set(0, new KeCheng("7","毛概"));
6     }

 

删除List中的元素有两种方法,一是使用remove方法

复制代码
 1 /*
 2      * 使用remove方法删除课程
 3      */
 4     public void testRemove(){
 5         KeCheng kc = (KeCheng) beixuan.get(0);
 6         System.out.println("我是课程:"+kc.id+":"+kc.name+";即将被删除");
 7         boolean b = beixuan.remove(kc);
 8         if(b){
 9             System.out.println("成功删除课程");
10             testForeach();
11         }
12     }
复制代码

 

还可以使用removeAll方法删除一个课程类型的数组

复制代码
 1 /*
 2      * 使用removeAll方法删除课程
 3      */
 4     public void testRemoveAll(){
 5         System.out.println("即将删除4和5位置上的课程");
 6         KeCheng[] kc = {(KeCheng) beixuan.get(4),(KeCheng) beixuan.get(5)};
 7         beixuan.removeAll(Arrays.asList(kc));
 8         System.out.println("课程删除成功");
 9         testForeach();
10     }
复制代码

猜你喜欢

转载自blog.csdn.net/qqtingshuo/article/details/80647574