Java 容器:Collection 初探之 List

   1 ///: JavaBasic//com.cnblogs.pattywgm.day1//CollectionTest.java
2
3 package com.cnblogs.pattywgm.day1;
4
5 import java.io.BufferedReader;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.ListIterator;
13
14 /**
15 * @author Administrator
16 * @Time: 2014-6-13
17 * @Descri: CollectionTest.java
18 */
19
20 public class CollectionTest {
21 //Testing of List
22 List<String> list1=new ArrayList<String>();
23 List<String> list2=new ArrayList<String>();
24 List<Integer> list3=new ArrayList<Integer>();
25
26 public CollectionTest() {
27 addElement();
28 }
29
30 public void addElement(){
31 int count=5;
32 while(count>=0){
33 //Appends the specified element to the end of list1
34 list1.add("goods"+count);
35 /*Insert the specified element in the head of list1
36 list1.add(0, "goods"+count);
37 */
38 count--;
39 }
40
41 }
42
43 public void addCollectionElements(){
44 //Appends all of the elements in list1 (the specified collection) to the end of list2
45 list2.addAll(list1);
46 }
47
48 public void getElement(List<String> list){
49 // 1)
50 for(int i=0;i<list.size();i++){
51 System.out.printf("%s's %dth element is %s",list,i,list.get(i).toString());
52 System.out.println();
53 }
54 System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
55 // 2)use Iterator
56 Iterator<String> iter=list.iterator();
57 int j=0;
58 while(iter.hasNext()){
59 System.out.printf("%s's %dth element is %s",list,j++,iter.next());
60 System.out.println();
61 }
62 }
63
64 public void removeElement(List<String> list,int index,String obj){
65 // 1) Removes the element at the specified position in this list
66 list.remove(index);
67 // 2) Removes the first occurrence of the specified element from this list, if it is present .
68 list.remove(obj);
69 /*
70 * Removes all of the elements from this list.
71 * The list will be empty after this call returns.
72 */
73 // list.clear();
74
75 }
76
77 public List<String> getSubList(List<String> list, int fromIndex, int toIndex){
78 return list.subList(fromIndex, toIndex);
79 }
80 //ListIterator
81 public void listIter(List<String> list){
82 ListIterator<String> listIter= list.listIterator();
83 //The element is inserted immediately before the next element that would be returned by next
84 //here the nexindex is 0
85 listIter.add("goods end");
86 System.out.println("previous index is :"+listIter.previousIndex()+" "
87 +"previous element is "+listIter.previous());
88 System.out.println("Change the last element of list...");
89 listIter.set("goods end changed");
90 System.out.println("正向遍历Starting...");
91 while(listIter.hasNext()){
92 System.out.println(listIter.next());
93 }
94 System.out.println("反向遍历Starting...");
95 while(listIter.hasPrevious()){
96 System.out.println(listIter.previous());
97 }
98 //Removes from the list the last element that was returned by next or previous
99 //Becareful: just one element every call
100 listIter.remove();
101 }
102
103 //add element : stdin
104 public void addElementStandard(List<Integer> list){
105 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
106 try {
107 String str[]=br.readLine().split(",");//split element with ','
108 for(String ss:str){
109 list.add(Integer.parseInt(ss));
110 }
111 } catch (IOException e) {
112 System.out.println("Error happended!!!");
113 e.printStackTrace();
114 }
115 }
116
117 public void sortList(List<Integer> list){
118 Object[] sortedList=list.toArray();
119 Arrays.sort(sortedList);
120
121 for(Object obj:sortedList){
122 System.out.print(obj+" ");
123 }
124 }
125 }
126 //:~

以上代码是对Java容器中List接口方法应用的各种实现,包括列表元素的增删改查。下面逐一做详细介绍:

1、增加列表元素

向列表中增加元素,总体来说有两类,即使用add()或addAll(),前者是向列表中直接插入指定的单个元素,而后者则是添加指定 collection 中的所有元素到此列表。

此外,可以指定向列表中插入元素的位置,这就将add()又细分为:add(E e)  add(int index, E element),前者默认在列表尾部增加元素,后者则在指定位置插入元素。

<addAll()的划分同add()>

2、删除列表元素

  Java API中,关于列表的删除操作有:remove()、removeAll()以及clear(),clear()将同时删除列表中的所有元素,但列表本身还是存在的,remove()依

据参数类型的不同,既可以实现删除指定位置的元素,也可以在不知道元素位置而知道列表中包含此元素时实现删除指定的元素,但此时只是删除第一次出现的指定元素,若

想删除列表中所有与指定元素相同的元素,可以循环调用remove(Object obj)直到列表中已不存在该元素。removeAll()则是实现从列表中移除指定 collection 中包含的

所有元素示例如下:

  

 List<String> lis=new ArrayList<String>();
lis.add("one");
lis.add("two");
lis.add("three");
lis.add("one");
lis.add("four");
lis.add("one");
lis.remove("one");
for(String s:lis){
System.out.println(s);
}
System.out.println("~~~~~~~~~~~~~~~");
while(lis.contains("one")){
lis.remove("one");
}
for(String s:lis){
System.out.println(s);
}

3、改变列表中已有元素

  改变列表中已有元素,主要通过set()方法实现,该方法包含两个参数,实现用指定元素(参数2)替换列表中指定位置(参数1)的元素。

4、查找列表元素

  同增删操作类似,查找也分为contains()和cotainsAll()两类,在此不再赘述。

【说明:】

  List接口所提供的还有其他很多方法,本文只说明了方法名称,未详细表明方法参数,具体请参照JAVA API。

上一篇:单元测试过多,导致The configured user limit (128) on the number of inotify instances has been reached.


下一篇:Java 容器(list, set, map)