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
length
after creating the Array object.
- One can not change the
ArrayList
is dynamic insize
.- Each
ArrayList
object has instance variable capacity which indicates the size of theArrayList
. - As elements are added to an
ArrayList
its capacity grows automatically.
- Each
-
Performance
- Performance of Array and
ArrayList
depends on the operation you are performing
-
Performance
Resize()
operation : Automatic resize ofArrayList
will 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
ArrayList
can only contain non-primitve data types (likeInteger
,Float
,Double
)ArrayList
can not contain primitive data types (likeint
,float
,double
)- Array can contain both primitive as well as non-primitive (
Object
s) data types.
-
Primitives
- Suppose we have
ArrayList
object,
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
ArrayList
is provided by thesize()
method - The number of elements in an array is provided by the
length
variable.
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
arraylistobject
using 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
-
add
andget
method: Performance of Array andArrayList
are similar for theadd
andget
operations.- Duplicate elements: Both Array and
ArrayList
can contain duplicate elements.- Unlike
Set
s andMap
s (no duplicate keys)
- Unlike
- Null Values: Both can store
null
values and uses index to refer to their elements. - Ordered: Both guarantee ordered elements.
- Unlike
HashMap
andHashSet
- 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();