標籤:數組實現 返回 iter sem end 個數 hashset 動態 ret
容器的概念
在Java當中,如果有一個類專門用來存放其它類的對象,這個類就叫做容器,或者就叫做集合,集合就是將若干性質相同或相近的類對象組合在一起而形成的一個整體。
容器中常用的方法
1 int size();返回大小 2 boolean isEmpty()非空判斷 3 boolean contains(Object o)判斷o在集合內是否存在 4 Iterator<E> iterator()集合迭代器 5 Object[] toArray()返回包含此容器中所有元素的數組 6 boolean remove(Object o)把o元素從集合內刪除 7 boolean add(Object obj)向容器中添加指定的元素 8 Object get(int index)擷取下標為index的那個元素 9 Object remove(int index)刪除下標為index的那個元素10 Object set(int index,Object element)將下標為index的那個元素置為element11 Object add(int index,Object element)在下標index的位置添加一個對象element12 Object put(Object key,Object value)向容器中添加指定的元素13 Object get(Object key)擷取關鍵字為key的那個對象
容器分類
容器可以分為Collection與Map兩種介面。List介面與Set介面繼承了Collection。
Set介面的實現為HashSet與TreeSet,List介面的實現為ArrayList與LinkedList,Map介面的實現為HashMap與TreeMap。
ArrayList常用方法
ArrayList實現了List介面,也叫動態數組,有三種建構函式,以下是源碼中常用的方法以及注釋。
1 //指定大小的建構函式 2 public ArrayList(int initialCapacity) { 3 if (initialCapacity > 0) { 4 this.elementData = new Object[initialCapacity]; 5 } else if (initialCapacity == 0) { 6 this.elementData = EMPTY_ELEMENTDATA; 7 } else { 8 throw new IllegalArgumentException("Illegal Capacity: "+ 9 initialCapacity); 10 } 11 } 12 //無參數建構函式 13 public ArrayList() { 14 this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; 15 } 16 //指定內容建構函式 17 public ArrayList(Collection<? extends E> c) { 18 elementData = c.toArray(); 19 if ((size = elementData.length) != 0) { 20 if (elementData.getClass() != Object[].class) 21 elementData = Arrays.copyOf(elementData, size, Object[].class); 22 } else { 23 this.elementData = EMPTY_ELEMENTDATA; 24 } 25 } 26//返回集合長度 27 public int size() { 28 return size; 29 } 30 //判斷集合內是否有值 31 public boolean isEmpty() { 32 return size == 0; 33 } 34 //判斷集合中是否存在對象o 35 public boolean contains(Object o) { 36 return indexOf(o) >= 0; 37 } 38 //返回包含此集合中所有元素的數組 39 public Object[] toArray() { 40 return Arrays.copyOf(elementData, size); 41 } 42 //擷取指定位置的元素 43 public E get(int index) { 46 return elementData(index); 47 } 48 //在指定位置替換元素 49 public E set(int index, E element) { 52 E oldValue = elementData(index); 53 elementData[index] = element; 54 return oldValue; 55 } 56 //添加元素 57 public boolean add(E e) { 58 59 elementData[size++] = e; 60 return true; 61 } 62 //在指定位置添加元素 63 public void add(int index, E element) {
69 elementData[index] = element; 70 size++; 71 } 72 //刪除指定位置的元素 73 public E remove(int index) { 74
77 E oldValue = elementData(index); 78 79 int numMoved = size - index - 1; 80 if (numMoved > 0) 81 System.arraycopy(elementData, index+1, elementData, index, 82 numMoved); 83 elementData[--size] = null; 84 85 return oldValue; 86 } 87 //在指定元素 88 public boolean remove(Object o) { 89 if (o == null) { 90 for (int index = 0; index < size; index++) 91 if (elementData[index] == null) { 92 fastRemove(index); 93 return true; 94 } 95 } else { 96 for (int index = 0; index < size; index++) 97 if (o.equals(elementData[index])) { 98 fastRemove(index); 99 return true;100 }101 }102 return false;103 }104 105 private void fastRemove(int index) {106 modCount++;107 int numMoved = size - index - 1;108 if (numMoved > 0)109 System.arraycopy(elementData, index+1, elementData, index,110 numMoved);111 elementData[--size] = null; 112 }
ArrayList數組擴容
每當執行Add、AddRange、Insert、InsertRange等添加元素的方法,都會檢查內部數組的容量是否不夠了,如果是,它就會以當前容量的兩倍來重新構建一個數組,將舊元素Copy到新數組中,然後丟棄舊數組,在這個臨界點的擴容操作,應該來說是比較影響效率的。
ArrayList用例
1 public class ArrayListUse { 2 public static void main(String[] args) { 3 List test = new ArrayList(); 4 5 test.add("1"); 6 test.add("2"); 7 test.add("3"); 8 test.add("4"); 9 test.add("5");10 11 test.add(0,5);12 System.out.println("指定位置add:"+test.get(0));13 System.out.println("指定位置add:"+test.get(1));14 15 test.remove(2);16 System.out.println("指定位置刪除"+test.get(2));17 System.out.println("長度"+test.size());18 19 20 }21 }
總結
ArrayList是用數組實現的一種集合類,是非安全執行緒的,所以,建議在單線程中才使用ArrayList,而在多線程中可以選擇Vector或者CopyOnWriteArrayList。
如有不對之處,歡迎留言指正!
Java容器-ArrayList