在 Java 中,ensureCapacity(int, minCapacity) 方法的作用是什麼?
java.util.ArrayList 類中的 ensureCapacity(int minCapacity) 方法會根據需要擴大此 ArrayList 例項的容量,以確保它至少可以容納最小容量引數指定的元素數量。
示例
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String args[]) {
ArrayList<Integer> arrlist = new ArrayList<Integer>(5);
arrlist.add(10);
arrlist.add(50);
arrlist.add(30);
arrlist.ensureCapacity(15);
for (Integer number : arrlist) {
System.out.println("Number = " + number);
}
}
}輸出
Number = 10
Number = 50
Number = 30
- 相關文章
- 在 Java 中,fill(int[], int val) 方法的作用是什麼?
- 在 Java 中,remove(int) 方法的作用是什麼?
- 在 Java 中,int capacity() 方法的作用是什麼?
- 在 Java 中,get(int) 方法的作用是什麼?
- 在 Java 中,removeRange(int fromIndex, int toIndex) 方法的作用是什麼?
- 在 Java 中,equals(int[] a1, int[] a2) 方法的作用是什麼?
- 在 Java 中,copyOf(int[] original, int newLength) 方法的作用是什麼?
- 在 Java 中,fill(int[], int fromIndex, int toIndex, int val) 方法的作用是什麼?
- 在 Java 中,sort(int[] a, int fromIndex, int toIndex) 方法的作用是什麼?
- 在 Java 中,copyOfRange(int[] original, int from, int to) 方法的作用是什麼?
- java 中 hashCode(int[] a) 方法有什麼用?
- java 中 sort(int[] a) 方法有什麼用?
- java 中 elementAt(int index) 方法有什麼用?
- java 中 removeElementAt(int index) 方法有什麼用?
- java 中 fill(obj[], int fromIndex, int toIndex, int val) 方法有什麼用?