java集合之List

1.  List的四个基本增删改查方法的使用如下:

 1 public class Demo1_List {
 2 
 3     /*
 4      * void add(int index, E element)
 5      * E remove(int index)     返回值为删除的元素
 6      * boolean remove(E element)
 7      * E get(int index)
 8      * E set(int index, E element)
 9      */
10     public static void main(String[] args) {
11 
12         //demo1();
13         //demo2();
14         //demo3();
15         List l = new ArrayList();
16         l.add("a");
17         l.add("b");
18         l.add("c");
19         l.set(1, "dd");         //将指定位置的元素修改
20         System.out.println(l);  //[a, dd, c]
21     }
22 
23     public static void demo3() {
24         List l = new ArrayList();
25         l.add("a");
26         l.add("b");
27         l.add("c");
28         //Object o = l.get(0);
29         //System.out.println(o);
30         //通过索引遍历list集合
31         for (int i = 0; i < l.size(); i++) {
32             System.out.println(l.get(i));
33         }
34     }
35 
36     public static void demo2() {
37         List l = new ArrayList();
38         l.add("a");
39         l.add("b");
40         l.add("c");
41         l.add(555);
42         Object obj = l.remove(1);
43         System.out.println(obj);  //b    
44         System.out.println(l);    //[a, c]
45         System.out.println(l.remove("a"));   //true
46         //Object o = l.remove(555);    //java.lang.IndexOutOfBoundsException: Index: 555, Size: 2   这里删除时不会自动装箱
47     }
48 
49     public static void demo1() {
50         List l = new ArrayList();
51         l.add("a");
52         l.add("b");
53         l.add("c");
54         System.out.println(l);    //[a, b, c]
55         l.add(3, "d");          //0 <= index <= size  时不会报异常
56         //l.add(4, "d");        java.lang.IndexOutOfBoundsException: Index: 4, Size: 3
57     }
58 
59 }

2.  使用get()  和  size()方法结合遍历存有自定义对象的集合

 1 public class Demo2_List {
 2 
 3     /*
 4      * 向List集合中存储学生对象,使用size()和get()结合的方法遍历集合
 5      */
 6     public static void main(String[] args) {
 7         List l = new ArrayList();
 8         l.add(new Student("张三",18));
 9         l.add(new Student("李四",19));
10         l.add(new Student("王五",10));
11         for (int i = 0; i < l.size(); i++) {
12 //            System.out.println(l.get(i));
13             Student s = (Student) l.get(i);
14             System.out.println(s.getName() + " " + s.getAge());
15         }
16     }
17 
18 }

3.  需要注意:遍历的同时修改集合会报java.util.ConcurrentModificationException并发修改异常,要想避免这个异常,则可以使用迭代器特有的添加方法

 1 public class Demo3_ConcurrentModificationException {
 2 
 3     public static void main(String[] args) {
 4 
 5         List l = new ArrayList();
 6         l.add("a");
 7         l.add("world");
 8         l.add("b");
 9         l.add("c");
10         
11         /*Iterator it = l.iterator();
12         while (it.hasNext()) {
13             String s = (String) it.next();
14             if (s.equals("world")) {
15                 l.add("javaee");      //遍历的同时修改集合会报java.util.ConcurrentModificationException并发修改异常
16             }                         //要想避免这个异常,则可以使用迭代器特有的添加方法
17         }
18         */
19         
20         ListIterator lit = l.listIterator();    //获取list集合特有的迭代器
21         while (lit.hasNext()) {
22             String s = (String)lit.next();
23             if (s.equals("world")) {
24                 lit.add("javaee");              //使用list特有的迭代器添加方法在遍历的同时添加元素不会出现并发修改异常
25             }
26         }
27         System.out.println(l);    //[a, world, javaee, b, c]
28         
29     }
30 
31 }

4.  Vector

 1 public class demo4_Vector {
 2 
 3     /*
 4      * 现在Vector已经基本上废弃,由list代替
 5      * 
 6      * 数组:查询修改快,增删慢
 7      * 
 8      * 链表:查询修改慢,增删改快
 9      * 
10      * list的三个子类比较:
11      *    ArrayList:
12      *        底层数据结构是数组,查询快,增删慢
13      *        线程不安全,效率高
14      *        
15      *    Vector:
16      *        底层数据结构是数组,查询快,增删慢
17      *        线程安全,,效率低
18      *    
19      *    Vector相对ArrayList查询慢(线程安全),相对LinkList增删慢(数据结构)
20      *    
21      *    LinkList:
22      *        底层数据结构是链表,查询慢,增删快
23      *        线程不安全,效率高
24      *        
25      *    Vector和ArrayList的区别:
26      *        Vector是线程安全的,效率低
27      *        ArrayList是线程不安全的,效率高
28      *        
29      *    ArrayList和LinkList的区别:
30      *        ArrayList底层是数组结构,查询和修改快
31      *        LinkList底层是链表结构,增删快,查询修改慢
32      *        
33      *    NB:
34      *        查询多时用ArrayList
35      *        增删多时用LinkList
36      *        如果都多的话,用ArrayList
37      *  
38      */
39     public static void main(String[] args) {
40 
41         Vector v = new Vector();
42         v.addElement("a");
43         v.addElement("b");
44         v.addElement("c");
45         
46         Enumeration en = v.elements();
47         while(en.hasMoreElements()) {
48             System.out.println(en.nextElement());
49         }
50     }
51 
52 }

5.  对List集合中的元素去重

 1 /**
 2  * @author Administrator
 3  * 对存储有重复字符串的集合去重
 4  */
 5 @SuppressWarnings({ "rawtypes", "unchecked" })
 6 public class Demo5_ArrayList_quchong {
 7 
 8     /**
 9      * @param args
10      */
11     public static void main(String[] args) {
12 
13         ArrayList list = new ArrayList();
14         list.add("a");
15         list.add("a");
16         list.add("b");
17         list.add("b");
18         list.add("b");
19         list.add("c");
20         list.add("c");
21         ArrayList li = getSingle(list);
22         System.out.println(li);
23     }
24     
25     
26     /**
27      * @param list
28      * @return ArrayList
29      * 思路:通过创建一个新的集合对老集合遍历,将需要留下的元素插入新集合中
30      * 
31      * 1. 创建一个新空集合
32      * 2.对老集合进行遍历,判断新集合中是否存在该元素,如果不存在则插入,如果存在则继续遍历
33      * 3.将新集合返回
34      */
35     public static ArrayList getSingle(ArrayList list) {
36         ArrayList newList = new ArrayList();
37         Iterator it = list.iterator();
38         while(it.hasNext()) {
39             Object o = it.next();
40             if (!newList.contains(o)) {
41                 newList.add(o);
42             }
43         }
44         return newList;
45     }
46 
47 }
48 
49 ***************************************************************
50 
51 /**
52  * @author Administrator
53  * 对自定义类集合去重处理
54  *
55  */
56 @SuppressWarnings({ "rawtypes", "unchecked" })
57 public class Demo6_ArrayList_zdyquchong {
58 
59     /**
60      * @param args
61      */
62     public static void main(String[] args) {
63 
64         ArrayList list = new ArrayList();
65         list.add(new Person("张三", 23));
66         list.add(new Person("张三", 23));
67         list.add(new Person("李四", 24));
68         list.add(new Person("李四", 24));
69         list.add(new Person("李四", 24));
70         
71         ArrayList li = getSingle(list);
72         System.out.println(li);
73     }
74 
75     /**
76      * @param list
77      * @return ArrayList
78      * 思路:通过创建一个新的集合对老集合遍历,将需要留下的元素插入新集合中
79      * 
80      * 1. 创建一个新空集合
81      * 2.对老集合进行遍历,判断新集合中是否存在该元素,如果不存在则插入,如果存在则继续遍历
82      * 3.将新集合返回
83      */
84     public static ArrayList getSingle(ArrayList list) {
85         ArrayList newList = new ArrayList();
86         Iterator it = list.iterator();
87         while(it.hasNext()) {
88             Object o = it.next();
89             if (!newList.contains(o)) {
90                 newList.add(o);
91             }
92         }
93         return newList;
94     }
95 }

猜你喜欢

转载自www.cnblogs.com/jiangjunwei/p/9211711.html