Thursday, February 27, 2014

Array List Tutorial

What is Array list?

Array list is a class that extends abstract list and implements the List interface. It will allow duplicates values.

Normal java array you have to set a size. But array list will support dynamic grow, that means it will be created with an initial size. After that if the size is exceeded, it will automatically increase the size. So Array Index out of bounds exception will not come.


The default value of array list size is 10. Now if you will add the 11th element half size will be increased half of its original size.

Remember, Array list is not synchronized. If you array list should be synchronized, use the below.

Collections.synchronizedList( new ArrayList());

It supports three constructors.

1.ArrayList()
2.ArrayList(Collection c)
3.ArrayList(int capacity)


Create Array List

You ca create an array list like below.


ArrayList<String> list = new ArrayList<String>(); //This Array List will Store only String objects

Adding values into array list


You can add the values into array list with the help of add method. 

list.add("java");
list.add("check");

Getting index of values


If you want to know the index of particular value, you have to pass that into the indexOf method. It will give the index. 

int index = list.indexOf("check");


Getting size of Array list values


If you want to know the size of the array list, you can use the size method.

int size = list.size();

Checking the array list is empty or not


If you want to check whether an array list is empty or not, use isEmpty method.
 
boolean result = list.isEmpty();

If the result value is true means, that's empty.Otherwise not.

Checking array list have an item


The conatains method will helpful to check array list have specified item or not.

boolean check = list.contains("java");

If the check value have true means, it have "java" value. Otherwise not.

Getting array list values through loop

With for loop you can get all values of array list.


for (String data: list) {
    System.out.println("The values are " + data);  
}

Removing an element from array list

To remove the specified object from list, you can use remove method.


list.remove("java");


Copying the data with ArrayList


addAll method will helpful for adding all list into another list.

ArrayList<String> listNew = new ArrayList<String>();
listNew.addAll(list);

Iterator and List Iterator with array list


Already we have seen, we can use enhanced for loop to get list values. Now you can use iterator and list Iterator to get values. See the below,

Iterator it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}

//Iterate with List Iterator

ListIterator listItr = list.listIterator();
while(listItr.hasNext()){
System.out.println(itr.next());
}

Simple array list example

package com.javahit.example;

import java.util.ArrayList;

public class JavaHitArrayList {

    public static void main(String[] args){

    //Creating arraylist... See I didn't mention any size
       
        ArrayList<String> al = new ArrayList<String>();
        //adding elements to the ArrayList
        al.add("one");
        al.add("two");
        al.add("three");
        al.add("four");
        System.out.println(al);

        //getting elements by index
        System.out.println("Index one value:"+al.get(1));


        //Checking the elements exist or not
        System.out.println("Checking existing value:"+al.contains("two"));

        //to add elements at index
        al.add(2,"PLAY");
       

        System.out.println("Checking array list empty "+al.isEmpty());
        System.out.println("Getting index value of two "+al.indexOf("two"));
        System.out.println("Arraylist size is: "+al.size());

        System.out.println(al);    
     }
}

No comments:

Post a Comment