ArrayList: It is a dynamic array which means we don’t need to give its size while creating it. Whenever we add or remove elements from an ArrayList its size dynamically increases or decreases.
Initialization:
- ArrayList arr = new ArrayList();
- ArrayList arr = new ArrayList(Collection c);
- ArrayList arr = new ArrayList(n);
List<Type> list = new ArrayList<>(); or ArrayList<Type> list = new ArrayList<>(); // Passing default size of arraylist ArrayList<Type> list = new ArrayList<>(20);
The default size of the array list: is 10
Load factor: 0.75 (when the list is filled to 75% it’s sized increases by 1.5 times)
ArrayList is implemented using Dynamic Array. Where when the load factor is encounted the size of the array is incresed x times.
Initialization
ArrayList<String> carsList = new ArrayList<>(); carsList.add("Porche 998 911"); carsList.add("Range Rover Evoque"); carsList.add("Range Rover Defender");
ArrayList<String> carsList = new ArrayList<String>() { { add("Porche 998 911"); add("Range Rover Evoque"); add("Range Rover Defender"); } };
List<Integer> numbersList = Arrays.asList(1, 2, 3,4);
Methods
Methods:
- add(value) or add(index, value); :- O(n) in worst case scenario
- get(index) :- O(1) Constant ; returns element at given index
- remove(index) :- O(n); removes element at given index
- indexOf(Object o):- O(n); returns first index of passed object/element
- contains(Object o) : O(n); returns true is passed object/ element is present
- size(): returns the size of the arraylist
add() : adds and element to the list
addAll(): adds another list to the current list
remove(): removes an element from the list
contains(): checks whether arraylist contains the element
clear(): clears the list
indexOf()
lastIndexOf()
removeAll()
size()
toArray()
Ques: How ArrayList dynamically allocate memory?
Ans: ArrayList arr = new ArrayList(); Default constructor is used to create empty ArrayList of initial size 10.
#Load Factor of an ArrayList
Diff between Array and ArrayList
Array | ArrayList |
Can’t update size once created | Can update size even after creation |
Can hold primitive and objects both | Can only hold object uses Autoboxing to auto convert primitive to objects |
Either can be 1D or 2D | Only 1D |
static | dynamic |
faster | slower |
Questions:
- How to synchronize ArrayList in java?
Ans: By using Collection.synchronizedList() method
- Difference between length() of array and size() of ArrayList in Java?
Ans: length() : returns how many elements you can store in the array
size(): returns total number of elements stored in arraylist
- When to use Array and when to use Linked List
- Difference between ArrayList and Vector?
Ans: Both implement List interface but
ArrayList: Not synchronised, not thread safe and slow
Vector: Synchronised, thread safe and fast
Output Type Questions:
Question 1.
import java.util.ArrayList;
public class ArrayListTest {
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
list.add(null);
list.add(0, “A”);
list.add(3, “B”);
list.add(1, “C”);
System.out.println(list);
}
}
Ans: It will compile but JVM will throw IndexOutOfBoundsException because we are adding “B” at location 3 which doesn’t exist.