安全程式設計(六)- Java集合類2
1.陣列(Array)和列表(ArrayList)有什麼區別?什麼時候應該使用Array而不是ArrayList?
Array可以包含基本資料型別和物件型別,ArrayList只能包含物件型別;
Array大小是固定的,ArrayList的大小是動態變化的;
ArrayList提供了更多的方法和特性,比如:addAll(),removeAll()等。
接下來我們詳細瞭解一下ArrayList:
(1).ArrayList簡介:
ArrayList是一個數組佇列,相當於動態陣列。與Array相比,它的容量可以動態增長。它繼承與AbstractList,實現了List介面,提供了相關的增加,刪除,修改,遍歷等功能;實現了RandomAccess介面,RandmoAccess是java中用來被List實現,為List提供快速訪問功能的;實現了Cloneable介面,即覆蓋了函式clone(),能被克隆;實現java.io.Serializable介面,這意味著ArrayList支援序列化,能通過序列化去傳輸。和Vector不同,ArrayList中的操作不是執行緒安全的!所以,建議在單執行緒中才使用ArrayList,而在多執行緒中可以選擇Vector或者CopyOnWriteArrayList。
(2).ArrayList的遍歷方式:
ArrayList支援三種遍歷方式:
(2.1)通過迭代器遍歷,即通過Iterator去遍歷;
Integer value = null; Iterator iter = list.iterator(); while (iter.hasNext()) { value = (Integer)iter.next(); }
(2.2)隨機訪問,通過索引值去遍歷;
Integer value = null;
int size = list.size();
for (int i=0; i<size; i++) {
value = (Integer)list.get(i);
}
(2.3)for迴圈遍歷;
Integer value = null;
for (Integer integ:list) {
value = integ;
}
例項:
package cn.nuist.pers.August27;
import java.util.*;
import java.util.concurrent.*;
public class ArrayListRandomAccessTest {
public static void main(String[] args) {
List list = new ArrayList();
for (int i=0; i<100000; i++)
list.add(i);
//isRandomAccessSupported(list);
iteratorThroughRandomAccess(list) ;
iteratorThroughIterator(list) ;
iteratorThroughFor2(list) ;
}
private static void isRandomAccessSupported(List list) {
if (list instanceof RandomAccess) {
System.out.println("RandomAccess implemented!");
} else {
System.out.println("RandomAccess not implemented!");
}
}
public static void iteratorThroughRandomAccess(List list) {
long startTime;
long endTime;
startTime = System.currentTimeMillis();
for (int i=0; i<list.size(); i++) {
list.get(i);
}
endTime = System.currentTimeMillis();
long interval = endTime - startTime;
System.out.println("iteratorThroughRandomAccess:" + interval+" ms");
}
public static void iteratorThroughIterator(List list) {
long startTime;
long endTime;
startTime = System.currentTimeMillis();
for(Iterator iter = list.iterator(); iter.hasNext(); ) {
iter.next();
}
endTime = System.currentTimeMillis();
long interval = endTime - startTime;
System.out.println("iteratorThroughIterator:" + interval+" ms");
}
public static void iteratorThroughFor2(List list) {
long startTime;
long endTime;
startTime = System.currentTimeMillis();
for(Object obj:list)
;
endTime = System.currentTimeMillis();
long interval = endTime - startTime;
System.out.println("iteratorThroughFor2:" + interval+" ms");
}
}
執行結果:
iteratorThroughRandomAccess:5 ms
iteratorThroughIterator:6 ms
iteratorThroughFor2:5 ms
由此可知,隨機訪問的效率最高,迭代器的效率最低。
(3).ArrayList示例:
package cn.nuist.pers.August27;
import java.util.*;
public class ArrayListTest {
public static void main(String[] args) {
// 建立ArrayList
ArrayList list = new ArrayList();
// 將“”
list.add("1");
list.add("2");
list.add("3");
list.add("4");
// 將下面的元素新增到第1個位置
list.add(0, "5");
// 獲取第1個元素
System.out.println("the first element is: "+ list.get(0));
// 刪除“3”
list.remove("3");
// 獲取ArrayList的大小
System.out.println("Arraylist size=: "+ list.size());
// 判斷list中是否包含"3"
System.out.println("ArrayList contains 3 is: "+ list.contains(3));
// 設定第2個元素為10
list.set(1, "10");
// 通過Iterator遍歷ArrayList
for(Iterator iter = list.iterator(); iter.hasNext(); ) {
System.out.println("next is: "+ iter.next());
}
// 將ArrayList轉換為陣列
String[] arr = (String[])list.toArray(new String[0]);
for (String str:arr)
System.out.println("str: "+ str);
// 清空ArrayList
list.clear();
// 判斷ArrayList是否為空
System.out.println("ArrayList is empty: "+ list.isEmpty());
}
}
執行結果:
the first element is: 5
Arraylist size=: 4
ArrayList contains 3 is: false
next is: 5
next is: 10
next is: 2
next is: 4
str: 5
str: 10
str: 2
str: 4
ArrayList is empty: true