Array vs Arraylist java

Array vs ArrayList java:

Array:
1. Size must be defined during initialization itself.
2. Size can not be resized once initialized.

[java]int[] myArraySize = new int[10]; //Possible
int[] myArray = new int[];  // Not possible[/java]

3. Size of the array calculated using length.

[java]int[] myArr = new int[9];System.out.println("size of the array : "+myArr.length); //prints 9[/java]

4. Both objects and primitives can be stored.

[java]int[] myArr = new int[9]; //Possible (primitive data type)
Integer[] myArrInteger = new Integer[10]; //Possible (wrapper class object) [/java]

 

ArrayList:
1. Size can be resized after initialized.
2. It is not mandatory to specify the size during initialization.

[java]ArrayList myArrayListSize = new ArrayList(5); //Possible, after initialization also size can be increased automatically
ArrayList myArrayList = new ArrayList(); //Possible[/java]

3. Size of the ArrayList calculated using size().

[java]
ArrayList myArrayListSize = new ArrayList(5); //Possible, after initialization also size can be increased automatically
ArrayList myArrayList = new ArrayList(); //Possible
[/java]

 

Only objects (wrapper class objects) can be stored in ArrayList (not only arraylist in all collections). But anyhow we can able to store the primitive data types using Autoboxing (available after java 1.5).

[java]
ArrayList myArrayList = new ArrayList(); //Not possible for primitives
ArrayList myArrayList = new ArrayList(); //Possible (wrapper class), primitives can be converted and stored automatically using autoboxing.
[/java]

 

ArrayList dynamically increase the size, here we are going to initialize arraylist with size 5, but once the 6th element inserted then it will automatically increases its size.

[java]
import java.util.ArrayList;
import java.util.Iterator;

public class ArrayVsArrayList {
public static void main(String[] args) {
ArrayList myArrayListSize = new ArrayList(5); //Possible, after initialization also size can be increased automatically
ArrayList myArrayList = new ArrayList(); //Possible

myArrayListSize.add("javadomain");
myArrayListSize.add("javadomain");
myArrayListSize.add("javadomain");
myArrayListSize.add("javadomain");
myArrayListSize.add("javadomain");
myArrayListSize.add("javadomain");
myArrayListSize.add("javadomain"); //size increased dynamically
Iterator myIter = myArrayListSize.iterator();
while(myIter.hasNext()){
System.out.println(myIter.next());
}
}

}

[/java]

Output:
javadomain
javadomain
javadomain
javadomain
javadomain
javadomain
javadomain

Size of the ArrayList can not be identified till the element added in the arraylist eventhough we specified it’s size,

[java]
int[] myArr = new int[9];

ArrayList myArrayListSize = new ArrayList(5); //Possible, after initialization also size can be increased automatically
ArrayList myArrayList = new ArrayList(); //Possible

System.out.println("size of the array : "+myArr.length); //prints 9
System.out.println("size of the arraylist : "+myArrayListSize.size()); //prints 0, once the element added then only the memory will be allocated.
[/java]

Output:
size of the array : 9
size of the arraylist : 0

 

 

 

Leave a Reply