In Java, following are two different ways to create an array.
- Array: Simple fixed sized arrays that we create in Java, like below
int arr[] = new int[10] - ArrayList : Dynamic sized arrays in Java that implement List interface.
ArrayList<Type> arrL = new ArrayList<Type>(); Here Type is the type of elements in ArrayList to be created
Differences between Array and ArrayList
- An array is basic functionality provided by Java. ArrayList is part of collection framework in Java. Therefore array members are accessed using [], while ArrayList has a set of methods to access elements and modify them.
// A Java program to demonstrate differences between array// and ArrayListimportjava.util.ArrayList;importjava.util.Arrays;classTest{publicstaticvoidmain(String args[]){/* ........... Normal Array............. */int[] arr =newint[2];arr[0] =1;arr[1] =2;System.out.println(arr[0]);/*............ArrayList..............*/// Create an arrayList with initial capacity 2ArrayList<Integer> arrL =newArrayList<Integer>(2);// Add elements to ArrayListarrL.add(1);arrL.add(2);// Access elements of ArrayListSystem.out.println(arrL.get(0));}}chevron_rightfilter_noneOutput:
1 1
- Array is a fixed size data structure while ArrayList is not. One need not to mention the size of Arraylist while creating its object. Even if we specify some initial capacity, we can add more elements.
// A Java program to demonstrate differences between array// and ArrayListimportjava.util.ArrayList;importjava.util.Arrays;classTest{publicstaticvoidmain(String args[]){/* ........... Normal Array............. */// Need to specify the size for arrayint[] arr =newint[3];arr[0] =1;arr[1] =2;arr[2] =3;// We cannot add more elements to array arr[]/*............ArrayList..............*/// Need not to specify sizeArrayList<Integer> arrL =newArrayList<Integer>();arrL.add(1);arrL.add(2);arrL.add(3);arrL.add(4);// We can add more elements to arrLSystem.out.println(arrL);System.out.println(Arrays.toString(arr));}}chevron_rightfilter_noneOutput:
[1, 2, 3, 4] [1, 2, 3]
- Array can contain both primitive data types as well as objects of a class depending on the definition of the array. However, ArrayList only supports object entries, not the primitive data types.
Note: When we do arraylist.add(1); : it converts the primitive int data type into an Integer object. Sample Code:importjava.util.ArrayList;classTest{publicstaticvoidmain(String args[]){// allowedint[] array =newint[3];// allowed, however, need to be initializedTest[] array1 =newTest[3];// not allowed (Uncommenting below line causes// compiler error)// ArrayList<char> arrL = new ArrayList<char>();// AllowedArrayList<Integer> arrL1 =newArrayList<>();ArrayList<String> arrL2 =newArrayList<>();ArrayList<Object> arrL3 =newArrayList<>();}}chevron_rightfilter_none - Since ArrayList can’t be created for primitive data types, members of ArrayList are always references to objects at different memory locations (See this for details). Therefore in ArrayList, the actual objects are never stored at contiguous locations. References of the actual objects are stored at contiguous locations.
In array, it depends whether the arrays is of primitive type or object type. In case of primitive types, actual values are contiguous locations, but in case of objects, allocation is similar to ArrayList. - Java ArrayList supports many additional operations like indexOf(), remove(), etc. These functions are not supported by Arrays.
.
.
.
As a side note, ArrayList in Java can be seen as similar to vector in C++.
This article is contributed by Pranjal Mathur. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- ArrayList of ArrayList in Java
- Java.util.ArrayList.add() Method in Java
- Java.util.ArrayList.addall() method in Java
- Java.util.Arraylist.indexOf() in Java
- Conversion of Array To ArrayList in Java
- Array of ArrayList in Java
- Array to ArrayList Conversion in Java
- Convert an ArrayList of String to a String array in Java
- Difference between length of Array and size of ArrayList in Java
- ArrayList to Array Conversion in Java : toArray() Methods
- ArrayList in Java
- ArrayList vs LinkedList in Java
- How to remove an element from ArrayList in Java?
- ArrayList and LinkedList remove() methods in Java with Examples
- Vector vs ArrayList in Java
- Synchronization of ArrayList in Java
- Custom ArrayList in Java
- Initialize an ArrayList in Java
- ArrayList toArray() method in Java with Examples
- Arraylist removeRange() in Java with examples

