Array and ArrayList
A tale of friendship, heartache and redemption
-
What we’ll cover
Differences Between Array and Arraylist
Similarities Between Array and Arraylist
Difference Between Array and Arraylist In Java
Common Array and Arraylist Tasks
-
Differences between Array and ArrayList
-
Resizable
- Array is static in size. Also known as a fixed length data structure.
    
- One can not change the 
lengthafter creating the Array object. 
 - One can not change the 
 ArrayListis dynamic insize.- Each 
ArrayListobject has instance variable capacity which indicates the size of theArrayList. - As elements are added to an 
ArrayListits capacity grows automatically. 
- Each 
 
-
Performance
- Performance of Array and 
ArrayListdepends on the operation you are performing 
-
Performance
Resize()operation : Automatic resize ofArrayListwill slow down the performance as it will use temporary array to copy elements from the old array to new array.
-
Performance
add() or get() operation : adding an element or retrieving an element from the Array or ArrayList object has almost same performance
-
Primitives
- One misconception is that we can store primitives (
int,float,double) inArrayList, but it is not true 
-
Primitives
ArrayListcan only contain non-primitve data types (likeInteger,Float,Double)ArrayListcan not contain primitive data types (likeint,float,double)- Array can contain both primitive as well as non-primitive (
Objects) data types. 
-
Primitives
- Suppose we have 
ArrayListobject, 
ArrayList<Integer>  arraylistobject = new ArrayList();
arraylistobject.add(23);  // try to add 23 (primitive)
-
ArrayList Autoboxing
JVM through Autoboxing (converting primitives to equivalent objects internally) ensures that only objects are added to the arraylistobject.
thus , above step internally works like this :
arraylistobject.add(new Integer(23));      
// Converted int primitive to Integer object and added to arraylistobject  
-
Length
- The number of elements in an 
ArrayListis provided by thesize()method - The number of elements in an array is provided by the 
lengthvariable. 
for example:
Integer arrayobject[] = new Integer[3];
arraylength= arrayobject.length; //uses arrayobject length variable
ArrayList<Integer>  arraylistobject = new ArrayList();
arraylistobject.add(12);
arraylistobject.size(); //uses arraylistobject size method
-
Adding elements
- We can insert elements into the 
arraylistobjectusing theadd()method. - We can insert elements into the array using the assignment operator.
 
for example:
Integer addarrayobject[] = new Integer[3];
addarrayobject[0]=new Integer(8); //new object is added to the array object
ArrayList<Integer>  arraylistobject = new ArrayList();
arraylistobject.add(12);
-
Multi-dimensional
- Array can be multi-dimensional, while ArrayList is always single dimensional.
 
for example:
Integer addarrayobject[][] = new Integer[3][2];
addarrayobject[0][0]= new Integer(8);
-
Similarities Between Array and ArrayList
-
addandgetmethod: Performance of Array andArrayListare similar for theaddandgetoperations.- Duplicate elements: Both Array and 
ArrayListcan contain duplicate elements.- Unlike 
Sets andMaps (no duplicate keys) 
 - Unlike 
 - Null Values: Both can store 
nullvalues and uses index to refer to their elements. - Ordered:  Both guarantee ordered elements.
    
- Unlike 
HashMapandHashSet 
 - Unlike 
 
-
Difference between Array and ArrayList in Java
-
| Array | ArrayList | |
|---|---|---|
| Resizable | No | Yes | 
| Primitives | Yes | No | 
| Iterating values | for, for each | Iterator,for each | 
| Length | length variable | size method | 
| Performance | Fast | Slow in comparision | 
| Multidimensional | Yes | No | 
| Add Elements | Assignment operator | add method | 
-
Common Array and Arraylist Tasks
-
Declaration and Instantiation
Array:
String[] villains = new String[5];
ArrayList:
ArrayList<String> heroes = new ArrayList<String>();
-
Add Items
Array:
villians[0] = "Dark Pheonix";
villians[1] = "Deadshod";
villians[2] = "CatWoman";
villians[3] = "Green Goblin";
villians[4] = "Poison Ivy";
ArrayList:
heros.add("Hellboy");
heros.add("Storm");
heros.add("Spawn");
heros.add("Silver Surfer");
heros.add("Mr. Fantastic");
-
Access an Item
Array:
villians[3];
ArrayList:
heroes.get(3);
-
Change an Item
Array:
vilians[4] = "Apocalypse";
ArrayList:
heroes.set(4, "The Tick");
-
Remove an Item
Array:
- There is no way to remove and item from an array.
 - You will need to duplicate values into a new array sans the value to remove.
 
ArrayList:
heroes.remove(0);
-
Clear an Item
Array:
- There is no way to clear an array.
 - You will need to create a new array
 
ArrayList:
heroes.clear();
-
Length of Items
Array:
villians.length;
ArrayList:
heroes.size();