[Java] What is ArrayList?

How to declare / initialize ArrayList

ArrayList is declared as follows. ArrayList Variable name = new ArrayList (initial capacity); * Initial capacity can be omitted

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

Why primitive types are bad

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?

Primitive type

//8 types
byte, short, boolean, char, int, long, float, double

What is the initial capacity specified for?

What happens when you do new, add, get, remove

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);

new.png

When the number of elements exceeds the initial capacity

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

new.png

How much the movement of the array affects

If not specified

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");

result

Processing time: 6505 ms

If specified

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");

result

Processing time: 5630 ms

There was a difference of about 1 second.

Conclusion

For large capacity, specify the expected capacity

Recommended Posts

[Java] What is ArrayList?
What is java
What is Java <>?
What is Java
What is Java Encapsulation?
What is Java technology?
What is Java API-java
[Java] What is flatMap?
[Java] What is JavaBeans?
What is Java Assertion? Summary.
What is a Java collection?
[Java] What is jaee j2ee?
[Java] What is class inheritance?
[Java basics] What is Class?
What is java escape analysis?
What is JVM (Java Virtual Machine)?
What is thread safe (with Java)
[Java] What is Concurrent Modification Exception?
What is Cubby
What is Docker?
What is null? ]
What is Keycloak
What is maven?
What is Jackson?
What is Docker
What is self
What is Jenkins
What is IM-Juggling?
What is params
What is Facade? ??
What is Gradle?
What is POJO
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is a class in Java language (3 /?)
What is the best file reading (Java)
What is a class in Java language (1 /?)
What is Java and Development Environment (MAC)
What is the main method in Java?
What is Maven Assembly?
What is `docker-compose up`?
What is vue cli
What is an interface?
What is the Java Servlet / JSP MVC model?
What is Ruby's self?
What is Ruby's attr_accessor?
What is permission denied?
What is instance control?
What is an initializer?
What is Spring Tools 4
What is an operator?
What is Guava's @VisibleForTesting?
What is MVC model?
What is an annotation?
What is Gradle's Artifact?
What is JPA Auditing?