ArrayList is one of the inbuilt data structures in Java. It is a dynamic array (where the size of the data structure not needed to be declared first) for storing elements (Objects).
AbstractList class and implements
List interface. An
ArrayList can contain duplicate elements where it maintains insertion order. It should be noted that the class
ArrayList is non-synchronized, so care should be taken when handling concurrency with
ArrayList allows random access because array works at the index basis. Manipulation is slow in
ArrayList because of shifting that often occurs when an element is removed from the array list.
ArrayList can be created as follows:
List<T> myArrayList = new ArrayList<>();
T ( Generics ) is the type that will be stored inside
The type of the
ArrayList can be any Object. The type can't be a primitive type (use their wrapper classes instead).
To add an element to the
Or to add item to a certain index:
myArrayList.add(index, element); //index of the element should be an int (starting from 0)
To remove an item from the
ArrayList, use the
Or to remove an item from a certain index:
myArrayList.remove(index); //index of the element should be an int (starting from 0)