Arayslist/list
What is a List
is an interface for ordered collections with a stable indexing order
allows duplicate elements to be inserted and provide a predictable iteration order
Has two implementations: ArrayList and LinkedList
What is an ArrayList
backed by a static array that has a fixed size.
it is a dynamic array
The size of the array is fixed on creation, but extended on add/remove etc operations, by creating a new array of greater length and copying the elements
The new size of the array is a geometric pattern, to allow for amortization of copying elments
Entries can be added to the array up to the maximum size of the backing array.
When the backing array is full, the class will allocate a new, larger array and copy the old values.
This affects performance
An ArrayList is initially backed by an empty array. On the first addition to the ArrayList a backing array of capacity 10 is allocated.
We can prevent this resizing behavior by passing our preferred initial capacity value to the constructor.
List<String> list = new ArrayList<>(1_000_000);Initializing the size on creation, improves insertion performance
Difference between Arrays and ArrayList
????
Operations on ArrayList
Creation
Creating new ArrayList
Creating an ArrayList with data, ie list of Integers
General
Check if empty
set up an empty list:
List<Integer> anArrayList = new ArrayList<>();
will return true if anArrayList is empty.
Clear a list
integerList.clear()removes all elements from list
Size of ArrayList
Retrieving an element using its index
integerList.get(0)returnz 1integerList.get(3)returnz 4integerList.get(sizeOfList - 1)returnz 4integerList.get(5)throws ArrayIndexOutOfBoundsException
Add single element to list
Add without mutating
Can add as many elements in
Stream.of(...)if wanted toAn alternative using flatMap:
Can add as many elements in
Arrays.asList(...)if wanted toCan use
Collections.singletonListinstead ofArrays.asList(...)
Add multiple elements to list
Add to an array of integers to the array:
System.out.println(anArrayList);returns [5, 1, 2, 3, 4] as anArrayList already has 5 in itAlso addAll adds to the end of the list
Replace an element at index
setup list:
List<Integer> integerList = Arrays.asList(1,2,3,4);
more code but does not mutate original list
Where 1 is the index to find element to replace
where 4 is the replacement
This will return [1,4,3,4]
Element exist in list
Find index of an element
Reverse a list
Sort list
Does not mutate the original list
Uses natural ordering, as Arraylist implements comparable
sublist of list
if anArrayList is [4,3,2,1,5]
Remove an element
Two ways of doing this, either by remove an element using the index, or getting the first element matched.
Without mutating
Removing the first specific element in the list
OR
Removing the element at a specific index in the list
Links
https://dzone.com/articles/the-developers-guide-to-collections-lists
https://docs.oracle.com/javase/8/docs/api/java/util/List.html
Last updated
Was this helpful?