Have you ever heard about data structure? If not, you might be interested to first know the definition before diving into the differences between array and arraylist.
Data structure is a collection of data elements. It can be any combination of memory, storage and data exchange in which the data is stored or transmitted.
Array | Arraylist |
Can be used to represent both primitives and objects. | Not able to store primitives in arraylist, but can store objects. |
Fixed-length data structure. | Variable-length collection class. |
Cannot be changed. | Can be changed. |
Array is a list of data elements that are accessible using an index number. The index number starts from 0 and continues to the end of the array. If you need to access the element at a particular index, you have to specify that index number in order to access it. The indexes are numbered from 0 up to but not including n-1, where n is the size of the array (n>=0).
A linked list is a linear sequence of nodes that contain information about the node itself and pointers to other nodes. In other words, linked lists contain pointers to other nodes. When you create a new node, it will be added at the end of the list with a pointer pointing to itself. The next pointer points back at this new node while previous pointers point forward to it.
Array is a fixed-length data structure, and arraylist is a variable-length collection class. So, once an array is created in Java, we cannot change the length of it, but it is possible to change the arraylist.
We are not able to store primitives in arraylist, but we can store objects. In Java, arrays can be used to represent both primitives and objects. Since Java 5, primitives are automatically transformed into objects, a process called auto-boxing.
In Java, array and arrayList are very common data structures. Arraylist is a class that is provided by Java. It is a class in the Java Collections Framework. It is part of the Java.util package.
Java is a programming language that is most often used to create applications and programs. It is designed for the creation of computer software, operating systems, client-server applications, applets, and enterprise. The language also allows the programmer to write reusable programs that can be shared by many applications.
Arraylist is a collection of objects that have been dynamically created. It is a container that contains a constant number of values that are of the same type. It has a shared memory location that is contiguous. Once we create a group, we cannot change it to become bigger.
A list of arrays in Java is internally backed by the array. It is possible to resize arrays in arraylist, but it can be a slow process as it involves copying content from one array to another.
In Conclusion
As you can see, arraylist is a collection of arrays, but the key difference is that it is a variable-length collection. You can change the size of an arraylist in Java and make it a a collection, but you cannot do that with an array. Arraylist also cannot store primitives, but we can store objects in an arraylist.