Blog Image

Array List  

Arraylist is the implementation class given for the List interface .It has some predefined methods . In this blog we can understand how to use these predefined methods and under which circumstances we should go for ArrayList. We can also understand under which data structure ArrayList works and how it works .

Collection (Interface):

    When we want to store multiple objects/elements of different class types in a single variable/entity then we should go for collection.

 Hence collection can be defined as a representation of heterogeneous (different type of) object as a single entity.

List is an interface which implements from collection.

List is a collection object where insertion order is preserved and duplicates are allowed.

List has some implementations

ArrayList:

  It is the implementation for the list interface. There are some features of ArrayList based on which we can understand under which circumstances we should go for ArrayList.

Features of ArrayList:

  •  It follows the growable or resizable data structure.
  • Insertion order is peserved and duplicates are allowed.
  • Heterogenous objects can be stored and null insertion is possible.

 It follows the growable or resizable data structure.

 When we create an object of ArrayList, it creates an empty  ArrayList object with index of default initial capacity as 10. As shown in figure.

    ArrayList arrayList=new ArrayList ();


 If we populate data and if it reaches index nine the data will be directly added to the cells of ArrayList but when we add the data for the index 10 it will create a new ArrayList with capacity with of   16((previous capacity*3/2)+1) index and copies the previous data in the new ArrayList as follows:

 Let consider the data is populated in the object arrayList



 After this if we again add data in arrayList object it will create new arrayList object with capacity 16 as shown below:


 


List interface defines the following specific methods.

 1) boolean add(int index,Object o);

 2) boolean addAll(int index,Collectio c); 

3) Object get(int index); 

4) Object remove(int index);

 5) Object set(int index,Object new);//to replace

 6) Int indexOf(Object o);                                                                                                    Returns index of first occurrence of ?o?.

 7) Int lastIndexOf(Object o);

 Now here is the program which gives us the perception how the data should populate in ArrayList and how the methods can be used provided in this collection type.


public class ALTest {

    public static void main(String args[]) {

        ArrayList arrayList = new ArrayList();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(null);
        arrayList.add("a");
        arrayList.add(4, 4);
        arrayList.add(5, "ac");
        arrayList.add(3.3);
        arrayList.add(null);
        arrayList.add(11);
        arrayList.add('f');
        arrayList.add(3);
        System.out.println(arrayList.contains(3.3)); /* true */

        System.out.println(
                arrayList); /* [1, 2, null, a, 4, ac, 3.3, null, 11, f] */

        System.out.println(arrayList.get(1)); /* 2 */

        System.out.println(arrayList.isEmpty()); /* false */

        System.out.println(arrayList.indexOf("ac")); /* 5 */

        ArrayList newArrayList = new ArrayList();
        newArrayList.add("one");
        newArrayList.add(2);
        newArrayList.add(arrayList);
        System.out.println(newArrayList);
        /* [one, 2, [1, 2, null, a, 4, ac, 3.3, null, 11, f, 3]] */

        newArrayList.addAll(1, arrayList);
        System.out.println(newArrayList);
        /*
         * [one, 1, 2, null, a, 4, ac, 3.3, null, 11, f, 3, 2, [1, 2, null, a,
         * 4, ac, 3.3, null, 11, f, 3]]
         */

        newArrayList.set(2, "element");
        System.out
                .println(newArrayList); /*
                                         * [one, 1, element, null, a, 4, ac,
                                         * 3.3, null, 11, f, 3, 2, [1, 2, null,
                                         * a, 4, ac, 3.3, null, 11, f, 3]]
                                         */

        newArrayList.removeAll(arrayList);
        System.out.println(newArrayList); /*
                                             * [one, element, [1, 2, null, a, 4,
                                             * ac, 3.3, null, 11, f, 3]]
                                             */
    }
}



From the above program we can understand that we can store heterogeneous objects in the arraylist and insertion order is preserved in arraylist .

We can also observe that null insertion is possible and duplicates are allowed in arraylist .

Now how the add(Object o) and add(int index, Object o) methods can be used.

 Also get(int index) will gives the value of the object which is present at that index.

isEmpty() returns true if the ArrayList created is empty and returns false if it is not empty as in above example.

contains(Object o) return true if our ArrayList objet contains the object o else it will return false.

indexOf(Object o) gives the index of object o.

We can add ArrayList inside the ArrayList by using add(collection c) or add(int index,collection c) as shown in the above example.

We can set the value of an index by set(int index,Object o).

We can remove any element by remove(object o) or remove(collection c) or remove(int index,object o).



There are different constructors are provided to create ArrayList object:

ArrayList()

It is used to create an empty array list.


ArrayList(Collection c)

It is used to create an array list that is initialized with the elements of the collection c.


ArrayList(int capacity)

It is used to create an array list that has the specified initial capacity.

We can create ArrayList object by using above constructor.


But when should we go for ArrayList ?

   ArrayList implements from RandomAccess interface which is marker interface. As we heve index for each element which is stored in our ArrayList object retrieving the data is easy job without any performance issue. So when we have data retrieving  scenario we should go for ArrayList.

  But when we have to add or remove any element/object from our ArrayList then there is performance issue because if we add any object in the middle of the arraylist from that index to the end every object has to shift their position.

 arrayList.add(4,?d?);


So the arraylist will becomes



 Similarly for removing any element/object again remaining elements from that index to end  has to shift.

arrayList.remove(4,?d?);

And the arrayList will becomes,


 So when it is retrieving operation recommends to go for ArrayList but when it is add or remove operation not to go for ArrayList.


Making the ArrayList synchronized:

ArrayList is non-synchronized and should not be used in multi-thread environment without explicit synchronization. Here collection has provided a method which returns synchronized list.

ArrayList arrayList=new ArrayList();

List synchornizedList=collections.synchronizedList(arrayList);


About author

User Image
AdityaT

I am a software developer. I am working working on jdk , jee technologies. I am working on Spring Framework as well. I am passionate about learning java technologies like Web Services and Restful Services.

2

-Comments

Be the first person to write a comment for this Blog
  • author image
    by:
      Bijay4
      20-7-2017 10:46:29 AM

    thanks buddy its very good demonstration keep update more collection related things

  • author image
    by:
      Abhishek3
      04-7-2017 09:25:38 PM

    thanks bro for the contribution it's helps me a lot. keep posting wonderful post

Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.