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.

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

3. Size of the array calculated using length.

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

4. Both objects and primitives can be stored.

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

 

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

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

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

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

 

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).

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

 

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.

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());
		}
	}

}

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,

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.

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

 

 

 

150 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *