In this article, we will discuss difference between Arrays and ArrayList in detail i.e.; Arrays v/s ArrayList
1. 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;
2. Arrays v/s ArrayList:
Arrays | ArrayList |
Arrays is fixed in length
For example, | 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
| For ArrayList iteration, use following options
|
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 |
3. 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]
4. 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]
Related Articles:
- Arrays class
- Arrays v/s ArrayList
- Java – Various ways to iterate Arrays in 5 ways
- Java – How to find duplicate in String Arrays ?
- Java – Various ways to remove duplicate elements from Arrays – 5 ways
- Java – Searching element from Arrays using Binary Search Algorithm
- Java – Creating ArrayList using nCopies method of Collections class
- Java – How to get size or length of an ArrayList ?
- Java – Adding element to ArrayList at specified index position
- Java – Remove element from ArrayList at specified index position
- Java – How to delete a element and delete all elements of an ArrayList ?
- Java- How to get sublist from ArrayList ?
- Java – How to check whether particular element is present in ArrayList ?
- Java – Comparing two ArrayList objects using containsAll() method
- Java – Adding one ArrayList to another ArrayList using addAll() method
- Java – Replacing ArrayList element with new value using set() method
- Java – How to get maximum element from ArrayList ?
References:
- https://www.benchresources.net/arraylist-class-in-java/
- https://www.benchresources.net/arrays-class-in-java/
- https://www.benchresources.net/sorting-arrays-using-comparable-and-comparator-in-java/
- https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Array.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
Happy Coding !!
Happy Learning !!