跳轉到內容

ArrayList

來自華夏公益教科書,開放的書籍,開放的世界

瀏覽 聚合 主題: v  d  e )

ArrayList 類擴充套件了 AbstractList 並實現了 List 介面。ArrayList 支援動態陣列,可以根據需要增長。

標準 Java 陣列具有固定長度。建立陣列後,它們無法增長或縮小,這意味著您必須事先知道陣列將容納多少個元素。

陣列列表以初始大小建立。當超過此大小時,集合將自動擴大。當刪除物件時,陣列可能會縮小。

初始化

[編輯 | 編輯原始碼]

ArrayList 類支援三個建構函式。第一個建構函式構建一個空的陣列列表。

ArrayList( )

以下建構函式構建一個用集合 c 中的元素初始化的陣列列表。

ArrayList(Collection c)

以下建構函式構建一個具有指定初始容量的陣列列表。容量是用於儲存元素的底層陣列的大小。

當元素新增到陣列列表時,容量會自動增長。

 ArrayList(int capacity)

ArrayList 定義了以下方法

在 ArrayList 中新增元素

[編輯 | 編輯原始碼]
  • 在列表中指定位置插入指定元素。該位置的現有元素及其下面的所有元素都會被移位(這與具有相同引數的 set 方法不同)。如果指定的索引超出範圍(index < 0 || index >= size()),則丟擲 IndexOutOfBoundsException
void add(int index, Object element)
  • 將指定元素追加到此列表的末尾。
boolean add(Object o)
  • 將指定集合中的所有元素追加到此列表的末尾,按指定集合的迭代器返回的順序。如果指定的集合為 null,則丟擲 NullPointerException
boolean addAll(Collection c)
  • 從指定位置開始,將指定集合中的所有元素插入到此列表中。如果指定的集合為 null,則丟擲 NullPointerException
boolean addAll(int index, Collection c)

ArrayList 的大小

[編輯 | 編輯原始碼]
  • 返回此列表中的元素數量。
int size()

新增元素和 ArrayList 的大小

 
import java.util.*;

public class ArrayListDemo{
	public static void main(String[] args) {
		// create an array list
		ArrayList <String> al= new ArrayList <String>();
		System.out.println("Initial ArrayList : "+al);
		
		 // add elements to the array list
		al.add("A");
		al.add("B");
		
		//find size of ArrayList
		System.out.println("Size of al :"+al.size());
		// display the array list
		System.out.println("Contents of al :"+al);
		al.add(1,"C");
		System.out.println("Contents of al :"+al);
		System.out.println("Size of al :"+al.size());
	}
}

新增元素和 ArrayList 大小的輸出

Computer code
Initial ArrayList : []
Size of al :2
Contents of al :[A, B]
Contents of al :[A, C, B]
Size of al :3

獲取和設定 ArrayList 元素

[編輯 | 編輯原始碼]
  • 返回此列表中指定位置的元素。如果指定的索引超出範圍(index < 0 或 index >= size()),則丟擲 IndexOutOfBoundsException
Object get(int index)
  • 用指定元素替換此列表中指定位置的元素。如果指定的索引超出範圍(index < 0 或 index >= size()),則丟擲 IndexOutOfBoundsException
Object set(int index, Object element)

查詢 ArrayList 元素的索引

[編輯 | 編輯原始碼]
  • 返回此列表中指定元素的第一次出現的索引,如果列表不包含此元素,則返回 -1。
int indexOf(Object o)
  • 返回此列表中指定元素的最後一次出現的索引,如果列表不包含此元素,則返回 -1。
int lastIndexOf(Object o)

查詢 ArrayList 中包含的元素

[編輯 | 編輯原始碼]
  • 如果此列表包含指定元素,則返回 true。更正式地說,當且僅當此列表包含至少一個元素 e 使得 (o==null ? e==null : o.equals(e)) 時,返回 true。
boolean contains(Object o)

ArrayList 中的不同方法

 
public class ArrayListDemo {
	public static void main(String[] args) {
		// create an array list
		ArrayList al = new ArrayList();

		// add elements to the array list
		al.add("A");
		al.add("B");
		al.add("C");
		al.add("A");
		al.add("D");
		al.add("A");
		al.add("E");
		System.out.println("Contents of al : " + al);

		// find index of element in ArrayList
		System.out.println("Index of D : " + al.indexOf("D"));
		System.out.println("Index of A : " + al.indexOf("A"));

		// find index of element in ArrayList
		System.out.println("Index of A : " + al.lastIndexOf("A"));

		// get element at given Index
		System.out.println("Element at Second Index : " + al.get(2));
		System.out.println("Element at Sixth Index : " + al.get(6));
		
		//set element at given Index
		al.set(3,"B"); // replacing third index element by "B"
		System.out.println("Contents of al : " + al);
		
		//check ArrayList contains given element
		System.out.println("ArrayList contain D : "+al.contains("D"));
		System.out.println("ArrayList contain F : "+al.contains("F"));
	}
}

ArrayList 中不同方法的輸出

Computer code
Contents of al : [A, B, C, A, D, A, E]
Index of D : 4
Index of A : 0
Index of A : 5
Element at Second Index : C
Element at Sixth Index : E
Contents of al : [A, B, C, B, D, A, E]
ArrayList contain D : true
ArrayList contain F : false
測試你的知識

問題: 考慮以下程式碼

Computer code
public class ArrayListDemo {
		public static void main(String[] args) {
	
		ArrayList al = new ArrayList();

		al.add("A");
		al.add("B");
		al.add("C");
		al.add("E");
		al.add("F");
	
		al.remove(2);
		al.remove("F");
		
		al.set(1, "G");
		al.add("H");
		al.set(3, "I");
		System.out.println("Size of al : " + al.size());
		System.out.println("Contents of al : " + al);

	}
  }


在上面的示例中,輸出是什麼?

答案
Computer code
Size of al : 4
Contents of al : [A, G, E, I]

更多 ArrayList 方法

方法 描述
Object clone() 返回此 ArrayList 的淺複製。
Object[] toArray() 返回一個包含此列表中所有元素的陣列,按正確順序排列。如果指定的陣列為 null,則丟擲 NullPointerException
void trimToSize() 將此 ArrayList 例項的容量修剪為列表的當前大小。
void ensureCapacity(int minCapacity) 如果需要,增加此 ArrayList 例項的容量,以確保它至少可以容納最小容量引數指定的元素數量。
protected void removeRange(int fromIndex, int toIndex) 從此列表中刪除所有索引在 fromIndex(含)和 toIndex(不含)之間的元素。
華夏公益教科書