Advantage of using ArrayList over Arrays
Drawbacks of Arrays:
- Arrays are fixed length, once created, you can not change the size of the array.
- After Arrays are created , you can not accommodate an extra element.
- Memory is allocated to an array during it’s creation only, much before the actual elements are added to it.
Advantages of ArrayList:
- You can define ArrayList as re-sizable array. Size of the ArrayList is not fixed. ArrayList can grow and shrink dynamically.
class ArrayListDemo{ public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("ONE"); list.add("TWO"); list.add("THREE"); System.out.println(list.size()); //Output : 3 //Inserting some more elements list.add("FOUR"); list.add("FIVE"); System.out.println(list.size()); //Output : 5 //Removing an element list.remove("TWO"); System.out.println(list.size()); //Output : 4 }}
- Elements can be inserted at or deleted from a particular position.
class ArrayListDemo{ public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("ZERO"); list.add("TWO"); list.add("FOUR"); System.out.println(list); //Output : [ZERO, TWO, FOUR] list.add(2, "THREE"); //Inserting an element at index 2 list.add(1, "ONE"); //Inserting an element at index 1 System.out.println(list); //Output : [ZERO, ONE, TWO, THREE, FOUR] list.remove(3); //Removing an element from index 3 System.out.println(list); //Output : [ZERO, ONE, TWO, FOUR] }}
- ArrayList class has many methods to manipulate the stored objects.
ArrayList class has methods to perform solo modifications ( add(), remove()… ), bulk modifications ( addAll(), removeAll(), retainAll()… ), searching( indexOf(), lasIndexOf() ) and iterations( iterator() ).
-
If generics are not used, ArrayList can hold any type of objects.
class ArrayListDemo{ public static void main(String[] args) { ArrayList list = new ArrayList(); //ArrayList without generics list.add("ZERO"); //adding string type object list.add(1); //adding primitive int type list.add(20.24); //adding primitive double type list.add(new Float(23.56)); //Adding Float wrapper type object list.add(new Long(25)); //Adding Long wrapper type object System.out.println(list); //Output : [ZERO, 1, 20.24, 23.56, 25] }}
- Many are of the assumption that multiple insertion and removal operations on ArrayList will decrease the performance of an application. But, there will be no significant change in the performance of an application if you use ArrayList instead of arrays. Below example shows time taken to add 1000 string elements to ArrayList and array.
class ArrayListDemo{ public static void main(String[] args) { String[] namesArray = new String[1000]; long startTime = System.currentTimeMillis(); for (int i = 0; i < namesArray.length; i++) { namesArray[i] = "Name"+i; } long endTime = System.currentTimeMillis(); System.out.println("Time taken by Array : "+(endTime - startTime)+"ms"); ArrayList<String> nameList = new ArrayList<String>(); startTime = System.currentTimeMillis(); for (int i = 0; i <= 1000; i++) { nameList.add("Name"+i); } endTime = System.currentTimeMillis(); System.out.println("Time taken by ArrayList : "+(endTime-startTime)+"ms"); }}
Output :
Time taken by Array : 6ms
Time taken by ArrayList : 6ms
- Many are of the assumption that multiple insertion and removal operations on ArrayList will decrease the performance of an application. But, there will be no significant change in the performance of an application if you use ArrayList instead of arrays. Below example shows time taken to add 1000 string elements to ArrayList and array.
-
You can traverse an ArrayList in both the directions – forward and backward using ListIterator.
class ArrayListDemo{ public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("ONE"); list.add("TWO"); list.add("THREE"); list.add("FOUR"); ListIterator iterator = list.listIterator(); System.out.println("Elements in forward direction"); while (iterator.hasNext()) { System.out.println(iterator.next()); } System.out.println("Elements in backward direction"); while (iterator.hasPrevious()) { System.out.println(iterator.previous()); } }}
-
ArrayList can hold multiple null elements.
class ArrayListDemo{ public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(100); list.add(null); list.add(null); System.out.println(list); //Output : [100, null, null] }}
-
ArrayList can hold duplicate elements.
class ArrayListDemo{ public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(100); list.add(100); list.add(100); System.out.println(list); //Output : [100, 100, 100] }}