Java入門系列:例項講解ArrayList用法
阿新 • • 發佈:2018-12-15
1.ArrayList建構函式
ArrayList類支援3個構造方法。
Arraylist()
這個構造方法構造了一個空的連結串列。
ArrayList(Collection<? extends E> c)
這個構造方法構造了一個包含指定元素集合的連結串列,注意,這裡的字元E是一個標記,用來表示集合中元素的型別。至於具體是什麼型別,需要你在使用這個構造方法的時候來指定。
ArrayList(int initialCapacity)
這是第三個構造方法,構造了一個指定大小但內容為空的連結串列。initialCapacity引數就是初始容量大小。
舉例來說,如果你要建立一個空的陣列連結串列,用來存放String型別的物件,那麼你可以像下面這樣做:
ArrayList<String> list = new ArrayList<String>();
如果你需要建立一個指定初始容量的陣列連結串列,你可以像下面這樣做:
ArrayList<Integer> list = new ArrayList<Integer>(7);
注意:ArrayList類只支援物件型別,不支援 基礎資料型別。就是說ArrayList物件只能存放物件,不能存放基礎資料型別的資料。
2.ArrayList常用方法
下面是總結了一些比較常用的ArrayList類成員方法:
- 增加元素到連結串列中
boolean add(Element e)
void add(int index, Element e)
增加指定元素到連結串列指定位置.
- 從連結串列中刪除元素
void clear()
從連結串列中刪除所有元素.
E remove(int index)
刪除連結串列中指定位置的元素.
protected void removeRange(int start, int end)
刪除連結串列中從某一個位置開始到某一個位置結束的元素。
- 獲取連結串列中的元素
E get(int index)
獲取連結串列中指定位置處的元素.
Object[] toArray()
- 修改某個元素
E set(int index, E element)
將連結串列中指定位置上的元素替換成新元素。
- 搜尋元素
boolean contains(Object o)
如果連結串列包含指定元素,返回true.
int indexOf(Object o)
返回元素在連結串列中第一次出現的位置,如果返回-1,表示連結串列中沒有這個元素。
int lastIndexOf(Object o)
返回元素在連結串列中最後一次出現的位置,如果返回-1,表示連結串列中沒有這個元素。
- 檢查連結串列是否為空
boolean isEmpty()
返回true表示連結串列中沒有任何元素.
- 獲取連結串列大小
int size()
返回連結串列長度(連結串列包含元素的個數).
以上是ArrayList類中使用比較多的成員方法。每個方法的更詳細的說明或其它沒有提及到的方法,讀者可以參考java官方api
3.ArrayList使用例項
1 import java.util.*; 2 3 public class ArrayListExamples { 4 5 public static void main(String args[]) { 6 // 建立一個空的陣列連結串列物件list,list用來存放String型別的資料 7 ArrayList<String> list = new ArrayList<String>(); 8 9 // 增加元素到list物件中 10 list.add("Item1"); 11 list.add("Item2"); 12 list.add(2, "Item3"); // 此條語句將會把“Item3”字串增加到list的第3個位置。 14 list.add("Item4"); 15 16 // 顯示陣列連結串列中的內容 17 System.out.println("The arraylist contains the following elements: " 18 + list); 19 20 // 檢查元素的位置 21 int pos = list.indexOf("Item2"); 22 System.out.println("The index of Item2 is: " + pos); 23 24 // 檢查陣列連結串列是否為空 25 boolean check = list.isEmpty(); 26 System.out.println("Checking if the arraylist is empty: " + check); 27 28 // 獲取連結串列的大小 29 int size = list.size(); 30 System.out.println("The size of the list is: " + size); 31 32 // 檢查陣列連結串列中是否包含某元素 33 boolean element = list.contains("Item5"); 34 System.out 35 .println("Checking if the arraylist contains the object Item5: " 36 + element); 37 38 // 獲取指定位置上的元素 39 String item = list.get(0); 40 System.out.println("The item is the index 0 is: " + item); 41 42 // 遍歷arraylist中的元素 43 44 // 第1種方法: 迴圈使用元素的索引和連結串列的大小 45 System.out 46 .println("Retrieving items with loop using index and size list"); 47 for (int i = 0; i < list.size(); i++) { 48 System.out.println("Index: " + i + " - Item: " + list.get(i)); 49 } 50 51 // 第2種方法:使用foreach迴圈 52 System.out.println("Retrieving items using foreach loop"); 53 for (String str : list) { 54 System.out.println("Item is: " + str); 55 } 56 57 // 第三種方法:使用迭代器 58 // hasNext(): 返回true表示連結串列連結串列中還有元素 59 // next(): 返回下一個元素 60 System.out.println("Retrieving items using iterator"); 61 for (Iterator<String> it = list.iterator(); it.hasNext();) { 62 System.out.println("Item is: " + it.next()); 63 } 64 65 // 替換元素 66 list.set(1, "NewItem"); 67 System.out.println("The arraylist after the replacement is: " + list); 68 69 // 移除元素 70 // 移除第0個位置上的元素 71 list.remove(0); 72 73 // 移除第一次找到的 "Item3"元素 74 list.remove("Item3"); 75 76 System.out.println("The final contents of the arraylist are: " + list); 77 78 // 轉換 ArrayList 為 Array 79 String[] simpleArray = list.toArray(new String[list.size()]); 80 System.out.println("The array created after the conversion of our arraylist is: " 81 + Arrays.toString(simpleArray)); 82 } 83 }