Difference between Array and ArrayList
- Arrays are created of fix size where ArrayList is dynamic in nature and can vary its length.so Array is a fixed length data structure while ArrayList is a variable length Collection class
- Size of Array cannot be incremented or decremented. But Size of ArrayList can be incremented or decremented as need.
- Once the Array is created elements cannot be added or deleted from it. But with ArrayList the elements can be added and deleted at runtime
- Elements of Array retrieved with for loop But Elements of ArrayList Can be retrieved with for loop and iterators both.
-
create ArrayList
ArrayList list = new ArrayList(); list.add(1); list.add(2); list.remove(3)
// remove the element from the 1st location.
create Array
int[] intArray= new int[3]; // one dimensional array
int[] intArray= new int[3][2]; // two dimensional array
int[] intArray= new int[3][2][1]; // three dimensional array
- ArrayList is one dimensional but array can be multidimensional.
int[][] intArray= new int[3][2]; // 2 dimensional array
- Array can contain objects of a single data type or class. ArrayList if not used with generic can contain objects of different classes.
- ArrayList can not contains primitive data types (like int , float , double) it can only contains Object while Array can contain both primitive data types as well as objects.
- We can insert elements into the Arraylist object using the add() method while in array we insert elements using the assignment operator.
In case of Array add element
Integer[] intarray= new Integer[3]; //create array of size 3
intarray[0]= new Integer(1);
intarray[1]= new Integer(3);
In case of ArrayList add element
ArrayList al= new ArrayList();
al.add(1);
al.add(2);
good content & easy to understand.
LikeLike