Wednesday, 31 December 2014

Java Programming Examples On ArrayList – 2


7) How do you get the position of a particular element in an ArrayList?

We can use indexOf() and lastIndexOf() methods to find out the position of a given element in an ArrayList. indexOf() method returns index of first occurrence of a specified element where as lastIndexOf() method returns index of last occurrence of a specified element in an ArrayList. If element is not found, they will return -1.

public class MainClass
{
    public static void main(String[] args)
    {
        ArrayList<String> list = new ArrayList<String>();

        list.add("JAVA");

        list.add("J2EE");

        list.add("JSP");

        list.add("JAVA");

        list.add("SERVLETS");

        list.add("JAVA");

        list.add("STRUTS");

        System.out.println(list);     //Output : [JAVA, J2EE, JSP, JAVA, SERVLETS, JAVA, STRUTS]

        //Getting the index of first occurrence of "JAVA"

        System.out.println(list.indexOf("JAVA"));     //Output : 0

        //Getting the index of last occurrence of "JAVA"

        System.out.println(list.lastIndexOf("JAVA"));    //Output : 5
    }
}

8) How do you convert an ArrayList to Array?

Using toArray() method of ArrayList class. toArray() method returns an array containing all elements of the ArrayList. This method acts as a bridge between normal arrays and collection framework in java.

public class MainClass
{
    public static void main(String[] args)
    {
        ArrayList<String> list = new ArrayList<String>();

        list.add("JAVA");

        list.add("J2EE");

        list.add("JSP");

        list.add("SERVLETS");

        list.add("STRUTS");

        System.out.println(list);      //Output : [JAVA, J2EE, JSP, SERVLETS, STRUTS]

        //getting an array containing all elements of the list.

        Object[] array = list.toArray();

        //Printing the elements of the returned array.

        for (Object object : array)
        {
            System.out.println(object);
        }

//        Output :

//        JAVA
//        J2EE
//        JSP
//        SERVLETS
//        STRUTS
    }
}

9) How do you retrieve an element from a particular position of an ArrayList?

get() method returns an element from a specified position of an ArrayList. This method takes index of the element as an argument.

public class MainClass
{
    public static void main(String[] args)
    {
        ArrayList<Integer> list = new ArrayList<Integer>();

        list.add(111);

        list.add(222);

        list.add(333);

        list.add(444);

        System.out.println(list);     //Output : [111, 222, 333, 444]

        //Getting element at index 3

        System.out.println(list.get(3));    //Output : 444

        //Getting element at index 1

        System.out.println(list.get(1));    //Output : 222
    }
}

10) How do you replace a particular element in an ArrayList with the given element?

set() method replaces a particular element in an Arraylist with the given element. This method takes two arguments. One is the index of the element to be replaced and another one is the element to be placed at that position.

public class MainClass
{
    public static void main(String[] args)
    {
        ArrayList<Integer> list = new ArrayList<Integer>();

        list.add(111);

        list.add(222);

        list.add(333);

        list.add(444);

        System.out.println(list);     //Output : [111, 222, 333, 444]

        //Replacing the element at index 1 with '000'

        list.set(1, 000);

        //Replacing the element at index 3 with '000'

        list.set(3, 000);

        System.out.println(list);   //Output : [111, 0, 333, 0]
    }
}

11) How do you append an element at the end of an ArrayList?

add() method appends an element at the end of an ArrayList.

public class MainClass
{
    public static void main(String[] args)
    {
        ArrayList<String> list = new ArrayList<String>();

        list.add("ONE");

        list.add("TWO");

        list.add("THREE");

        list.add("FOUR");

        System.out.println(list);     //Output : [ONE, TWO, THREE, FOUR]
    }
}


12) How do you insert an element at a particular position of an ArrayList?

add() method which takes index and an element as arguments can be used to insert an element at a particular position of an ArrayList. The elements at the right side of that position are shifted one position right i.e indices of right side elements of that position are increased by 1.

public class MainClass
{
    public static void main(String[] args)
    {
        ArrayList<String> list = new ArrayList<String>();

        list.add("ONE");

        list.add("TWO");

        list.add("THREE");

        list.add("FOUR");

        System.out.println(list);     //Output : [ONE, TWO, THREE, FOUR]

        //Inserting "AAA" at index 1

        list.add(1, "AAA");

        //Inserting "BBB" at index 3

        list.add(3, "BBB");

        System.out.println(list);    //Output : [ONE, AAA, TWO, BBB, THREE, FOUR]
    }
}

13) How do you remove an element from a particular position of an ArrayList?

remove() method which takes int type as an argument is used to remove an element from a particular position of an ArrayList.

public class MainClass
{
    public static void main(String[] args)
    {
        ArrayList<String> list = new ArrayList<String>();

        list.add("AAA");

        list.add("BBB");

        list.add("ccc");

        list.add("DDD");

        list.add("e");

        System.out.println(list);     //Output : [AAA, BBB, ccc, DDD, e]

        //Removing an element from position 2

        list.remove(2);

        System.out.println(list);    //Output : [AAA, BBB, DDD, e]

        //Removing an element from position 3

        list.remove(3);

        System.out.println(list);   //Output : [AAA, BBB, DDD]
    }
}

14) How do you remove the given element from an ArrayList?

remove(Object obj) method removes the first occurrence of the specified element ‘obj‘. If that element doesn’t exist, ArrayList will be unchanged.

public class MainClass
{
    public static void main(String[] args)
    {
        ArrayList<String> list = new ArrayList<String>();

        list.add("AAA");

        list.add("BBB");

        list.add("AAA");

        list.add("CCC");

        list.add("BBB");

        System.out.println(list);     //Output : [AAA, BBB, AAA, CCC, BBB]

        //Removing first occurrence of "AAA"

        list.remove("AAA");

        System.out.println(list);    //Output : [BBB, AAA, CCC, BBB]

        //Removing first occurrence of "BBB"

        list.remove("BBB");

        System.out.println(list);   //Output : [AAA, CCC, BBB]
    }
}

15) How do you remove all elements of an ArrayList at a time?

clear() method removes all elements of an ArrayList. ArrayList will be empty after this method is executed.

public class MainClass
{
    public static void main(String[] args)
    {
        ArrayList<String> list = new ArrayList<String>();

        list.add("AAA");

        list.add("BBB");

        list.add("AAA");

        list.add("CCC");

        list.add("BBB");

        System.out.println(list);     //Output : [AAA, BBB, AAA, CCC, BBB]

        //Removing all elements of the list

        list.clear();

        System.out.println(list);    //Output : []
    }
}

16) How do you retrieve a portion of an ArrayList?

Using subList() method of ArrayList, we can retrieve a portion of an ArrayList. subList() method returns a view of a portion of an ArrayList in the given range. The returned subList is backed by original ArrayList. That means any changes made to subList will be reflected in original ArrayList or Vice-Versa.

public class MainClass
{
    public static void main(String[] args)
    {
        ArrayList<Integer> list = new ArrayList<Integer>();

        list.add(111);

        list.add(222);

        list.add(333);

        list.add(444);

        list.add(555);

        list.add(666);

        System.out.println(list);     //Output : [111, 222, 333, 444, 555, 666]

        //Retrieving a SubList

        List<Integer> subList = list.subList(1, 4);

        System.out.println(subList);    //Output : [222, 333, 444]

        //Modifying the list

        list.set(2, 000);

        //Changes will be reflected in subList

        System.out.println(subList);    //Output : [222, 0, 444]

        //Modifying the subList

        subList.set(2, 000);

        //Changes will be reflected in list

        System.out.println(list);    //Output : [111, 222, 0, 0, 555, 666]
    }
}

No comments:

Post a Comment