In this article, we will discuss difference between Arrays and ArrayList in detail i.e.; Arrays v/s ArrayList
Difference will be based on 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 key differences between these Arrays & ArrayList
Arrays
ArrayList
Arrays is fixed in length
For example,
int iArr = new int[7];
ArrayList uses dynamic resizable/grow-able array
For 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 allowed
But 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 the size of an ArrayList
For Array iteration, use following options
for-loop
enhanced for-loop
For ArrayList iteration, use following options
for-loop
enhanced for-loop
Iterator
ListIterator
forEach from Java 8
Performance-wise, it always stays constant over time
add() & get() operations nearly provide same performance as that of Array
But with modify operation like deletion will yield poor performance because it involves lot of shifting
With 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 on Arrays and ArrayList:
Example 1: Arrays Sort operation
PrimitveNaturalSortingOfArrays.java
Output:
Example 2: ArrayList operation
ArrayListAddAndRemove.java
Output:
References:
http://www.benchresources.net/arraylist-class-in-java/
http://www.benchresources.net/arrays-class-in-java/
http://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 !!