ArrayList is declared as follows.
ArrayList
ArrayListSample.java
ArrayList<Integer> arrayList = new ArrayList<Integer>(); //No initial capacity specified
ArrayList<Integer> arrayList = new ArrayList<Integer>(3); //Specify the initial capacity
//ArrayList<int> arrayList = new ArrayList<int>(); //Compile error
Take a look at the contents of the ArrayList
ArrayList.class
public class ArrayList<E> extends AbstractList<E> … {
private static final int DEFAULT_CAPACITY = 10;
private static final Object[] EMPTY_ELEMENTDATA = {};
transient Object[] elementData; //← Actual condition of ArrayList data
private int size;
//constructor
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
}
}
//Omission
The identity of ArrayList was a convenient operation class centered on __Object [] __. Only __Object type data types can be used as ArrayList. __ Since the primitive type is not a data type that inherits the Object type, can it not be used?
//8 types
byte, short, boolean, char, int, long, float, double
ArrayList<Integer> arrayList = new ArrayList<Integer>(3);
arrayList.add(new Integer(10); //a
arrayList.add(new Integer(11); //b
arrayList.add(new Integer(12); //c
arrayList.get(1);
arrayList.remove(1);
ArrayList<Integer> arrayList = new ArrayList<Integer>(3);
arrayList.add(new Integer(10); //a
arrayList.add(new Integer(11); //b
arrayList.add(new Integer(12); //c
arrayList.add(new Integer(13); //d
ArrayListSample.java
ArrayList<Integer> arrayList = new ArrayList<Integer>();
long array1Start = System.currentTimeMillis();
for(int i=0; i < 10000000;i++) { //← 10 million
arrayList.add(new Integer(10));
}
long array1End = System.currentTimeMillis();
System.out.println("processing time:" + (array1End - array1Start) + " ms");
Processing time: 6505 ms
ArrayListSample.java
ArrayList<Integer> arrayList = new ArrayList<Integer>(10000000);
long array1Start = System.currentTimeMillis();
for(int i=0; i < 10000000;i++) { //← 10 million
arrayList.add(new Integer(10));
}
long array1End = System.currentTimeMillis();
System.out.println("processing time:" + (array1End - array1Start) + " ms");
Processing time: 5630 ms
There was a difference of about 1 second.
For large capacity, specify the expected capacity
Recommended Posts