Skip to the content.

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

-

Performance

-

Performance

-

Performance

add() or get() operation : adding an element or retrieving an element from the Array or ArrayList object has almost same performance

-

Primitives

-

Primitives

-

Primitives

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

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

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

for example:

Integer addarrayobject[][] = new Integer[3][2];
addarrayobject[0][0]= new Integer(8);

-

Similarities Between Array and ArrayList

-

  1. add and get method: Performance of Array and ArrayList are similar for the add and get operations.
  2. Duplicate elements: Both Array and ArrayList can contain duplicate elements.
    • Unlike Sets and Maps (no duplicate keys)
  3. Null Values: Both can store null values and uses index to refer to their elements.
  4. Ordered: Both guarantee ordered elements.
    • Unlike HashMap and HashSet

-

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:

ArrayList:

heroes.remove(0);

-

Clear an Item

Array:

ArrayList:

heroes.clear();

-

Length of Items

Array:

villians.length;

ArrayList:

heroes.size();