Difference between Array and ArrayList
Similarities
- Array and ArrayList both are used for storing elements.
- Array and ArrayList both can store null values.
- They can have duplicate values.
- They do not preserve the order of elements.
The following table describes the key differences between array and ArrayList:
Basis | Array | ArrayList |
---|---|---|
Definition | An array is a dynamically-created object. It serves as a container that holds the constant number of values of the same type. It has a contiguous memory location. | The ArrayList is a class of Java Collections framework. It contains popular classes like Vector, HashTable, and HashMap. |
Static/ Dynamic | Array is static in size. | ArrayList is dynamic in size. |
Resizable | An array is a fixed-length data structure. | ArrayList is a variable-length data structure. It can be resized itself when needed. |
Initialization | It is mandatory to provide the size of an array while initializing it directly or indirectly. | We can create an instance of ArrayList without specifying its size. Java creates ArrayList of default size. |
Performance | It performs fast in comparison to ArrayList because of fixed size. | ArrayList is internally backed by the array in Java. The resize operation in ArrayList slows down the performance. |
Primitive/ Generic type | An array can store both objects and primitives type. | We cannot store primitive type in ArrayList. It automatically converts primitive type to object. |
Iterating Values | We use for loop or for each loop to iterate over an array. | We use an iterator to iterate over ArrayList. |
Type-Safety | We cannot use generics along with array because it is not a convertible type of array. | ArrayList allows us to store only generic/ type, that's why it is type-safe. |
Length | Array provides a length variable which denotes the length of an array. | ArrayList provides the size() method to determine the size of ArrayList. |
Adding Elements | We can add elements in an array by using the assignment operator. | Java provides the add() method to add elements in the ArrayList. |
Single/ Multi-Dimensional | Array can be multi-dimensional. | ArrayList is always single-dimensional. |
原文地址:https://www.javatpoint.com/difference-between-array-and-arraylist