Class1 Esteem ArrayList
Class1 Esteem ArrayList
Collections are having dynamically grow-able nature, even if we add the elements over its size then
JVM will not rise any exception.
=======================================
In Java, by default, Arrays are able to allow homogeneous elements, if we are trying to add the
elements which are not same Array data type then Compiler will
rise an error like "Incompatible Types".
In Java, by default, Collections are able to allow heterogeneous elements, even we add different types
of elements Compiler will not rise any error.
=======================================
Eg: If we are given a number n and we want to find all the Fibonacci numbers until that number, then
we cannot use arrays as we have no knowledge about the number of elements before the given n. In
these type of scenarios ArrayList comes handy.
List:
ArrayList:
• It is not synchronized.
• No method is synchronized method in ArrayList.
• It allows more than one thread to access data.
• It follows parallel execution.
• It will reduce execution time.
• It will improve application performance.
• It will not give guarantee for data consistency.
• It is not thread safe.
• It is not Legacy Collection.
ArrayList sample code depicting the most commonly used methods: (copy in VSCode or OnlibeGDB
for better understanding)
import java.util.ArrayList;
import java.util.Iterator;
Special points:
1. If we store integer values in ArrayList and then we perform list.remove(x) then the integer will be
treated as index and not element.,i.e., the number at index x will be removed and not element x.
2. No error is thrown if we apply list.clear() in empty list.
.add()
.addAll()
.remove()
.removeAll()
.contains()
.containsAll();
.size()
.clear()
.isEmpty()
.toArray()