Last Updated on April 24, 2023 by Abhishek Sharma
In this article we’ll see the Array vs ArrayList. The data structures Array vs ArrayList are well-known in Java. While ArrayList is a class of the Java Collections framework, an array is a fundamental Java feature. It is a part of the java.util package.
Java Array
An object that is formed dynamically is an array. It functions as a holding area for a certain number of values of the same kind. It has an uninterrupted memory space. We are unable to alter an array’s size once it has been established. The next sentence may be used to create an array:
int array[]=new int[size];
An array of the requested size is created by the previous command. It produces an ArrayIndexOutOfBoundsException when we attempt to add more than its size. For instance
int arr[]=new int[3]; //specified size of the array is 3
//adding 4 elements into array
arr[0]=12;
arr[1]=2;
arr[2]=15;
arr[3]=67;
Java ArrayList Class
In Java, ArrayList is a class of Collections framework. It implements List, Collection, Iterable, Cloneable, Serializable, and RandomAccess interfaces. It extends AbstractList class.
We can create an instance of ArrayList by using the following statement:
ArrayList arrayList=new ArrayList();
In Java, the array serves as the internal backing for ArrayList. The ArrayList’s resize action slows down speed since it creates a new array and copies data from an old array to the new array. It uses System.arraycopy(sec, srcPos, dest, destPos, length) which is a natively implemented function.
Primitive types cannot be stored in an array list. Thus, it only stores objects. Primitive types are automatically converted to objects. For instance, we created an object called an ArrayList.
ArrayList list=new ArrayList(); //object of ArrayList
arrayObj.add(12); //trying to add integer primitive to the ArrayList
Through automatic boxing, the JVM transforms it into an integer object.
ArrayList arrayObj=new ArrayList()//object of ArrayList
arrayObj(new Integer(12)); /ArrayList object is inserted when the raw integer is converted to an integer object.
Similarities Between an Array and Arraylist
- Array vs ArrayList is used to store elements.
- Both Array vs ArrayList can contain null values.
- They may contain identical values.
- The arrangement of the components is not maintained.
Difference Between Array and ArrayList
The fundamental distinctions between Array vs ArrayList are shown in the following table:
Basis | Array | ArrayList |
---|---|---|
Definition | An object that is formed dynamically is an array. It functions as a holding area for a certain number of values of the same kind. It has an uninterrupted memory space. | A class in the Java Collections framework is the ArrayList. It includes well-known classes like HashTable, Vector, and HashMap. |
The static/ Dynamic | Size of an array is fixed. | The size of an ArrayList list is dynamic. |
Resizable | A fixed-length data structure is an array. | A data structure with a changeable length is an ArrayList. If necessary, it can resize itself. |
Initialization | When directly or indirectly initializing an array, the size must be provided. | An ArrayList object can be created without a size being specified. A default-sized ArrayList is created by Java. |
Performance | Due to its constant size, it performs quickly compared to ArrayList. | In Java, the array serves as the internal support for ArrayList. Performance is slowed down by the ArrayList’s resize process. |
Primitive/ Generic type | Both primitives and objects can be stored in an array. | Primitive types cannot be stored in an ArrayList. Primitive types are automatically converted to objects. |
Iterating Values | To iterate through an array, we use a for loop or a for each loop. | To iterate through an ArrayList, we need an iterator. |
Type-Safety | Because it is not a convertible type of array, we cannot utilize generics with the array. | it Because ArrayList only lets us store generic/type data, it is type-safe. |
Length | The length of an array is indicated by a length variable that is provided by the array. | To calculate the size of an ArrayList, use the size() function provided by ArrayList. |
Adding Elements | The assignment operator can be used to add elements to an array. | The add() function in Java allows you to add elements to an ArrayList. |
Single/ Multi-Dimensional | Array can be multi-dimensional. | ArrayList is always single-dimensional. |
Array example in Java:
In the example below, we have just constructed an array with a length of four.
class ArrayExample { public static void main(String args[]) { //creating an array of integer type int arr[]=new int[4]; //adding elements into array arr[0]=120; arr[1]=20; arr[2]=150; arr[3]=670; for(int i=0;i<arr.length;i++) { System.out.println(arr[i]); } } }
Output
120
20
150
670
ArrayList example in Java:
In the example that follows, we have created an instance of an ArrayList and iterated through it.
import java.util.*; class ArrayListExample { public static void main(String args[]) { //creating an instance of ArrayList List<Float> list = new ArrayList<Float>(); //adding element to arraylist list.add(12.4f); list.add(34.6f); list.add(56.8f); list.add(78.9f); //iteration over ArrayList using for-each loop for(Float f:list) { System.out.println(f); } } }
Output
12.4
34.6
56.8
78.9
Conclusion
I Hope this article was able to give you a clear understanding of Array vs Arraylist and the differences between them.
Also, you can go through with these similar differences: