Arrays v/s ArrayList

In this article, we will discuss difference between Arrays and ArrayList in detail i.e.; Arrays v/s ArrayList

 

Difference will be based on below parameters,

  • Size (fixed or variable)
  • Data type to be stored (primitive type or Object)
  • Data-type bounded using Generics
  • Adding or inserting or assigning elements
  • Length or size
  • Iterating through the elements

Lets us move on and discuss key differences between these Arrays & ArrayList;

 

Arrays v/s ArrayList:

Arrays ArrayList
Arrays is fixed in length

For example,
int iArr = new int[7];

ArrayList uses dynamic resizable/grow-able
arrayFor example,
ArrayList al = new ArrayList();
It allows to store primitive types & Objects It allows to store only Object whereas
primitive types like int, float, double,
etc aren’t allowedBut its equivalent wrapper Object
types
like Integer, Float, Double, etc
are allowed
While adding elements to Array, type is bounded i.e.; it allows to store element of any specific data-type or specific class

Trying to add another data-type, other than declared data-type results in throwing ArrayStoreException at runtime

Using Generics while declaring ArrayList
makes it is type-bounded i.e.; if ArrayList
is declared to accept only String or any specific
class then adding any other type results in
throwing compile-time error
Storing elements inside Array is easy, as simple assignment operator is enough

For example, intArr[0] = 10;

For adding element to ArrayList,
use add() or addAll() methods of java.util.Collection interface
For Array, length variable provides the length of an Array For ArrayList, size() method of
java.util.Collection interface
can be used to determine
size of an ArrayList
For Array iteration, use following options

  1. for-loop
  2. enhanced for-loop
For ArrayList iteration, use following
options

  1. for-loop
  2. enhanced for-loop
  3. Iterator
  4. ListIterator
  5. forEach from Java 8
Performance-wise, it always stays constant over time add() & get() operations nearly provide
same performance as that of ArrayBut with modify operation like deletion
will yield poor performance because
it involves lot of shiftingWith capacity reaching maximum
will result in again poor performance
as it involves copying data from old array
into new array
Example: Refer Arrays for details Example: Refer ArrayList for details

 

Example 1: Arrays Sort operation

PrimitveNaturalSortingOfArrays.java

package in.bench.resources.java.collection;

import java.util.Arrays;

public class PrimitveNaturalSortingOfArrays {

	public static void main(String[] args) {

		Integer[] intArrays = {31, 83, 53, 97, 29, 7, 13,  47, 79};
		String[] strArrays = {"Karthi", "Vikram", "Vijay", 
				"Simbhu", "Suriya", "Ajith"};

		System.out.println("Before sorting: Integer Arrays\n");

		// printing Integer Arrays
		System.out.println(Arrays.toString(intArrays));

		// sorting Arrays using
		Arrays.sort(intArrays);

		System.out.println("\nAfter sorting: Integer Arrays\n");

		// printing Integer Arrays
		System.out.println(Arrays.toString(intArrays));


		System.out.println("\n\n\nBefore sorting: String Arrays\n");

		// printing Integer Arrays
		System.out.println(Arrays.toString(strArrays));

		// sorting Arrays using
		Arrays.sort(strArrays);

		System.out.println("\nAfter sorting: String Arrays\n");

		// printing Integer Arrays
		System.out.println(Arrays.toString(strArrays));
	}
}

Output:

Before sorting: Integer Arrays

[31, 83, 53, 97, 29, 7, 13, 47, 79]

After sorting: Integer Arrays

[7, 13, 29, 31, 47, 53, 79, 83, 97]



Before sorting: String Arrays

[Karthi, Vikram, Vijay, Simbhu, Suriya, Ajith]

After sorting: String Arrays

[Ajith, Karthi, Simbhu, Suriya, Vijay, Vikram]

 

Example 2: ArrayList operation

ArrayListAddAndRemove.java

package in.bench.resources.java.collection;

import java.util.ArrayList;

public class ArrayListAddAndRemove {

	public static void main(String[] args) {

		// creating ArrayList object of type String
		ArrayList<String> al = new ArrayList<String>();

		// adding elements to ArrayList object
		al.add("Ajith Kumar");
		al.add("Vijay Joseph");  
		al.add("Karthi Sivakumar");  
		al.add("Vikram Kennedy");
		al.add("Dhanusk K Raja");
		al.add("Suriya Sivakumar");

		System.out.println("Iterating ArrayList values\n");

		// Iterating using enhanced for-loop
		for(String str : al){
			System.out.println(str);
		}

		// removing element at 4th index
		al.remove(4);

		// to print all values of ArrayList
		System.out.println("\n\nArrayList values after"
				+ " removal at 4th index postion \n\n" + al);
	}
}

Output:

Iterating ArrayList values

Ajith Kumar
Vijay Joseph
Karthi Sivakumar
Vikram Kennedy
Dhanusk K Raja
Suriya Sivakumar


ArrayList values after removal at 4th index postion 

[Ajith Kumar, Vijay Joseph, Karthi Sivakumar, 
Vikram Kennedy, Suriya Sivakumar]

 

References:

 

Happy Coding !!
Happy Learning !!

Sorting ArrayList using sort() method of List in Java 8
Java Collection Interview question and answers