這是一個老祖宗,一代一代往下撥
collection 的方法如下,是一個跟接口方法如下,見API
?
collection ?:
add():添加一個元素
addAll():添加一組元素
clear();清空
remove(Object o) :移除
removeAll():移除一組元素
isEmpty();判斷集合中是否有元素,沒有返回true
iterator():獲得迭代器
size():返回集合長度
??
List
允許重復
存入順序與取出順序一致:有序
有序的
允許空值
允許重復的元素
get(int index):獲得
set(int index, E element):修改
?
List下面已知的子類
ArrayList:單鏈表數據結構:查詢速度,線程不同步
LinkedList :雙鏈表數據結構:插入與刪除速度
Vector:線程同步
addFirst(E e)
addLast
removeFirst
removeLast
ArrayList取出數據的三種基本方法
1 import java.util.ArrayList; 2 import java.util.Iterator; 3 4 public class ArraylistDemo { 5 public static void main(String[] args) { 6 ArrayList<Integer> list = new ArrayList<Integer>(); 7 list.add((int) 'e'); 8 list.add(2); 9 list.add(3); 10 list.add(2); 11 list.add(null); 12 Iterator<Integer> i = list.iterator(); //迭代器迭代 13 while (i.hasNext()) { 14 Object s = i.next(); 15 System.out.println(s); 16 } 17 for (int j = 0; j < list.size(); j++) { //for循環 18 System.out.println(list.get(j)); 19 } 20 for (Integer integer : list) { //加強for循環 21 System.out.println(integer); 22 } 23 } 24 }
?
Vector
vector和ArrayList操作基本一樣,Vector相當于ArrayList的舊版. ?Vector線程是安全的.在Vector下有一個枚舉(Enumeration)的方法,和ArrayList下的迭代器(Iterator)功能一樣 ?
? ?
Set與Map見下節