Tuesday 13 December 2016

Difference: ArrayList Vs Array

ArrayList and Array differ based on following parameters:

1.       Nature
Array is static in nature. Its length is predefined and thus array cannot grow or shrink as per need. If it gets full then we cannot add more element to it.

ArrayList is dynamic in nature thus grows or shrink automatically as per the requirement.


2.       Memory Consumption
If element is removed from Array, the memory consumption doesn’t decrease as array doesn’t shrink automatically. It remains fixed even if element is removed thus holding memory without providing any proper utilization.

If element is removed from ArrayList, it automatically shrinks thus saving programmer from the issue of wasting memory.


3.       Provision of predefined methods
ArrayList provides lots of predefined methods which helps in making programmer task easier while implementing code thus gaining advantage over Array.

      4.       Example


Array Example:

package array;

public class ArrayExample {

      public static void main(String[] args) {
            int array[] = new int[5];
           
            for(int i=0;i<=5;i++){
                  array[i]=i*i;
                  System.out.println(i+" index contains "+ array[i]);
            }

      }

}


OUTPUT:
0 index contains 0
1 index contains 1
2 index contains 4
3 index contains 9
4 index contains 16
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
      at array.ArrayExample.main(ArrayExample.java:9)


Why is the exception thrown here?

Reason: We have declared array which has capacity to store 5 elements at max. So it stores 5 elements without any complaint(from index 0 to 4). But when we are trying to store value (5*5) at index array[5] we are sure to get ArrayIndexOutOfBoundsException: 5
To store one more element we need to have array of size 6. So here you have to change size like this:

int array[] = new int[6];

Now save the program and run again to see 25 getting stored at index 5.


ArrayList Example

package javaRadarArrayList;

import java.util.ArrayList;

public class SimpleArrayList {

      public static void main(String[] args) {
            //create Array List
            ArrayList<String> javaRadarList = new ArrayList<String>();
            //Add elements to ArrayList
            javaRadarList.add("Java");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            System.out.println(javaRadarList);
      }

}

OUTPUT:
[Java, Spring, Hibernate, EJB]

In ArrayList we don't need to declare size. It automatically allocates a default size first. And later if required arraylist automatically re-sizes itself.

No comments:

Post a Comment

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.