Difference Between Array vs ArrayList in Java: In this article, we will discuss the difference between Arrays and ArrayList in detail i.e.; Array vs ArrayList in Java
The difference will be based on the following 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 the key differences between these Array vs ArrayList in Java;
Array vs ArrayList in Java
Arrays |
ArrayList |
Arrays are fixed in length for example, int arr = new int[7]; | ArrayList uses a dynamic resizable/grow-able array for example, ArrayList al = new ArrayList(); |
It allows storing primitive types & Objects | It allows storing the only Object whereas primitive types like int, float, double, etc aren’t allowed but its equivalent wrapper Object types like Integer, Float, Double, etc are allowed |
While adding elements to Array, a 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 a 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 the size of an ArrayList |
For Array iteration, use the following options
| For ArrayList iteration, use the following options
|
Performance-wise, it always stays constant over time | add() & get() operations nearly provide the same performance as that of ArrayBut with modifying operation like deletion will yield poor performance because it involves a lot of shifting
With capacity reaching maximum will result in again poor performance as it involves copying data from the old array into the new array |
Example: Refer Arrays for details | Example: Refer ArrayList for details |
Example of Array vs ArrayList in Java
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]