Last Updated: 15 January, 2023
ArrayList is a class of the Collection Framework, and it implements the List interface.
Whereas, An array is an object in Java that contains the same data type of values. Its length is fixed once created and cannot be changed.
There are many differences between the ArrayList and Array that are given below.
ArrayList in Java is a concrete class of the List interface that was introduced in the JDK 1.2 version and is available in the java.util package. An ArrayList is used to store a dynamically sized collection of elements. An ArrayList grows in size automatically. Elements can be added, modified, and removed from an ArrayList whenever there is a need.
Java ArrayList Class Declaration
Here, E defines the type of elements that the List will contain.
Java ArrayList important features:
An array is an object in Java that contains the same data type of values. Its length is fixed once created and cannot be changed.
An Array allocates contiguous memory location for each element in Java.
Each item in an array is called an element, and each element is accessed by its unique numerical index value which starts from 0 which means the first element of the array will be stored at the 0th index position, 2nd element will be stored on the 1st index position and so on. With the help of the index number, we can directly access any random value from the array.
Java Array Syntax:
ArrayList | Array |
---|---|
The size of an arrayList is dynamic and can be increased or decreased as needed. | Array size is fixed. Once declared, its size can’t be changed. |
ArrayList can only be one dimension. | An array can be single or multidimensional. |
ArrayList can only hold reference types (objects), not primitives. | An array can hold both primitives and reference types (objects) in Java. |
ArrayList supports generics. | Array does not support generics. |
When compared to an array, an ArrayList performs worse and consumes more memory. | Array provides better performance and uses less memory. |
ArralyList provides various ways for iteration, like loops, Iterator and ListIterator class. | We can iterate through an array using loops only. |
ArrayList elements are not stored at contiguous memory locations. | Array elements are stored at contiguous memory locations. |
We can add elements to the ArrayList using the add () method. | Using the assignment operator, we can add elements to the array. |
Output
ArrayList Elements: [India, Canada, US, England]
Output
Index: 0, Element: A
Index: 1, Element: B
Index: 2, Element: C
Index: 3, Element: D
Index: 4, Element: E
Index: 5, Element: F
Index: 6, Element: G
Index: 7, Element: H
Index: 8, Element: I
Index: 9, Element: J
As given above example, here we are declaring 10 characters size of Array which means Array length is 10 and storing values like A, B, C, D, E, F, G, H, I, J. Since at the time of array declaration, the character 'A' is declared first and then 'B' character and so on, therefore 'A' will be store at the 0 index position and 'B' will be store at index 1 and so on as we can see in the below image.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com