Online Tutorials & Training Materials | STechies.com
Register Login

Difference between Array and ArrayList in Java with Example

|| 0

Difference between Array and ArrayList in Java with Example
Stechies

In case you have been confused about the difference between Array and ArrayList, then what follows is surely for you. This article has been structured to throw light upon the definition of Array and ArrayList, their examples, differences in Java. Once you have gone through the various sections, you will be in a much better position to figure out when to use an ArrayList or Array in the process of programming codes.

ArrayList vs Array Comparision Table

Here is the brief of ArrayList vs Array in tabular form:

Basis of Differentiation Array ArrayList
Size of data structure An array contains data structure of fixed length. The size of the Array cannot be changed once the object has been defined. It is static in nature. The size of an ArrayList is dynamic in nature. The elements/ items in the data structure can be modified to change the size of the object as and when required.
Resizing property As an Array length is static across the program, its size will remain unchanged. The size of an ArrayList is capable of changing dynamically on the basis of the capacity and load it has to function with.
Insertion and storage of elements The assignment operator is put to use for the storage of elements. The add() attribute is utilized for the insertion of elements in an ArrayList.
Nature of datatypes An Array can store primitive datatypes as well as other objects that are of the different or same data type. ArrayLists can only store object types. They are not able to contain primitives.
Generics Generics are not compatible with Arrays. ArrayLists allow the use of Generics.
Multi-dimensional nature Arrays are multi-dimensional. ArrayLists are single-dimensional.
Storage in contiguous memory locations An Array is a native programming component wherein the elements are contained in contiguous memory locations.
An ArrayList belongs to a class belonging to Java’s collections framework. Here, the objects are incapable of being contained in contiguous locations.
Determination of length The Length variable is responsible for determining an Array’s length.
The length of an ArrayList is set by the Size () method.
Memory space consumed Arrays take more memory for the storage of specified objects or elements.
ArrayLists take less memory space for storing objects or elements.
Iteration Iterating across an Array takes lesser time than what it does in case of ArrayLists.
Iterating across an ArrayList takes more time and the performance is slower.

Introduction of Array

Before we dive into the answers related to what is the difference between Array and ArrayList, it would help to get an introduction to what is an Array and ArrayList.

Arrays are strongly-typed data collections of the same type. They present a fixed length which is not possible to be altered during runtime. The Array elements are best accessed through the usage of numeric indexing that starts at zero. While the value of the reference elements are kept at null, the default value of the numeric array elements is usually set to zero.

Example of Array

Int[ ] intArray =new int [ ] {2};
intArray [0] = 1;
intArray [2] = 2;

Introduction ArrayList

An Array list is different from an Array as it is not in the nature of a strongly-typed collection. It is capable of storing data types that are either similar or different in nature. The overall size and nature of the Array list would decrease or increase dynamically to capture values of all sizes, and that too from any kind of data type. An ArrayList features among the most flexible of data structures in the C# Collections. It presents a simple and easy-to-implement list of values.

When you are using the ArrayList, you will find it implementing the IList interface that is compatible with Arrays. You can use it to modify, add, insert, delete or view the data types inserted by you. The ArrayList is extremely flexible as it grows dynamically and allows the addition of information of any size without any fuss or ado.

Example of an Array List

ArrayList Arrlst = new ArrayList ( );
Arrlst.Add (“Uma”);
Arrlst.Add (“1”);
Arrlst.Add (“null”);

Array vs ArrayList

Difference between array list and array in Java programming language is an important interview question that ascertains whether a candidate has knowledge about the dynamic and static nature of arrays or not. The difference between the two can be best understood and remembered on the basis of 8 comparative points: size, performance, primitives, iterator, type safety, length, adding elements and multi-dimensions.

1. Size:

Arrays are static in their length and size and boast of fixed data structures. It is not possible to change their length once the array object has been created by the developer. They contain similar datatype items/elements that have been defined in a sequential manner. The arrays in C/ C++ are known to behave in a different manner than how they behave in Java. On the other hand, an Array List is dynamic in nature. An ArrayList object will showcase an instance of variable capacity that appropriately depicts the size of the ArrayList. The capacity of the ArrayList is extensible and keeps growing as and when more variables are added to the same. This happens automatically.

DIfference Between Array and ArrayList

2. Performance:

The overall performance capabilities of the ArrayList and Array is dependent on the type of operation performed by the program user. For instance, in the

resize() operation: Automatic resizing of ArrayList decreases the performance speed of the operation as it uses a temporary array for copying elements to the new array from the old one.

When you use an Array list, it is backed internally by an Array in the process of calling the native implemented method as given below:

System.arrayCopy(src,srcPos,dest,destPos,length)

add() or get() operation: The procedure of retrieving an element from or adding any element from the ArrayList or Array object projects the same performance levels. As far as the ArrayList object is concerned, the operations would be carried out in constant time.

Overall, an ArrayList is slower in comparison to an Array.

3. Primitives:

Another difference between ArrayList and array in Java is that an ArrayList is not equipped to incorporate primitive data types such as int, float, double, etc. It contains objects only. On the other hand, Arrays are designed to contain both objects and primitive data types alike.

4. Iteration of values:

It is possible to iterate through the values provided to an ArrayList by using an iterator. The iterators that are returned by an ArrayList class's list iterator are fail-fast.On the other hand, the commands ‘for each loop’ or ‘for loop’ are used by Java developers to iterate through an Array.

5. Type-safety:

Java developers find it easy to ensure the programming language’s type-safety feature with the help of Generics. Here, it is important to understand that an Array comprises of tightly-typed similar or homogeneous data structure. Given this, it is possible for an Array to contain objects belonging to a specific class only; additionally, it may possess primitives belonging to specific data types only. It is not possible to store different data, apart from the ones specified in the initial stage of the object creation, at any stage. In case you attempt to do so, the ArrayStoreException error will be thrown back. This is not true in case of ArrayLists.

For instance:

String temp[] =  new String[3];  

This will generate a string array of size 3

temp[0] = new Integer(12); 

This command will throw ArrayStoreException. This is because the code is attempting to include an Integer object in String[ ].

6. Length:

The size( ) allows users to define the length of the ArrayList. On the other hand, Array objects include the length variable that fetches the length of the specific array.

For instance,

This command uses the arrayobject length variable

Integer arrayobject [ ] = new Integer[6];
arraylength= arrayobject.length; 

This command uses the ArrayList object size method

ArrayList  arraylistobject = new ArrayList();
arraylistobject.add(10); 
arraylistobject.size( );  

7. Addition of elements:

Elements can be inserted into the ArrayList object by using the add( ) method. In the case of an Array, elements can only be inserted with the help of an assignment operator.

For instance,

Integer addarrayobject[ ] = new Integer[8];
addarrayobject[0]= new Integer(3);  

This command will allow for a new object to be added to the specified array object. This difference of array to ArrayList in Java is one of the most important ones.

8. Multi-dimensional:

An ArrayList will always be single-dimensional even as an Array is capable of being multi-dimensional.

Here is an instance of a multidimensional array:

Integer addarrayobject[ ][ ] = new Integer[5][6];
addarrayobject [0][0]= new Integer(8)  

Similarities Between ArrayList and Array in Java

1. Add and get method: The performance levels of both are the same in the case of the add and get method. These operations will run in constant time.

2. Duplicate elements: Duplicate elements are contained by both Array and ArrayList.

3. Null Values: Both objects can contain null values; they use an index for making references to their elements.

4. Unordered: Array and ArrayList fail to guarantee ordered elements.