Wednesday, 31 December 2014

Java Programming Examples On ArrayList – 1


1) Explain the different ways of constructing an ArrayList?

ArrayList can be created in 3 ways.

a) ArrayList() —> It creates an empty ArrayList with initial capacity of 10.

b) ArrayList(int initialCapacity) —> It creates an empty ArrayList with supplied initial capacity.

c) ArrayList(Collection c) —> It creates an ArrayList containing the elements of the supplied collection.

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

        ArrayList<String> list2 = new ArrayList<String>(20);         //Second Method

        ArrayList<Integer> list3 = new ArrayList<Integer>(list1);      //Third Method
    }
}

2) How do you increase the current capacity of an ArrayList?

ensureCapacity() method can be used to increase the current capacity of an ArrayList. However, capacity of an ArrayList is automatically increased when we try to add more elements than the current capacity. To manually increase the current capacity, ensureCapacity() method is used.

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

        //Now 'list' can hold 10 elements (Default Initial Capacity)

        list.ensureCapacity(20);

        //Now 'list' can hold 20 elements.
    }
}

3) How do you decrease the current capacity of an ArrayList to the current size?

trimToSize() method is used to trim the capacity of arrayList to the current size of ArrayList. Developers use this method to minimize the storage area of an ArrayList.

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

        //Now 'list' can hold 10 elements (Default Initial Capacity)

        list.ensureCapacity(20);

        //Now 'list' can hold 20 elements.

        list.add("ONE");

        list.add("TWO");

        list.add("THREE");

        list.add("FOUR");

        //reducing the current capacity to current size of an ArrayList.

        list.trimToSize();
    }
}

4) How do you find the number of elements present in an ArrayList?

Using size() method. size() method returns number of elements present in an ArrayList.


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

        list.add(1.1);

        list.add(2.2);

        list.add(3.3);

        list.add(4.4);

        list.add(5.5);

        System.out.println(list);     //Output : [1.1, 2.2, 3.3, 4.4, 5.5]

        System.out.println("Size Of ArrayList = "+list.size());   //Output : Size Of ArrayList = 5
    }
}

5) How do you find out whether the given ArrayList is empty or not?

isEmpty() method of ArrayList can be used to check whether the given ArrayList is empty or not. This method returns true if an ArrayList contains no elements otherwise returns false.

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

        System.out.println(list.isEmpty());    //Output : true
    }
}

Note : You can also use size() method to check whether the given ArrayList is empty or not. size() method returns ‘0’ if an ArrayList is empty.

6) How do you check whether the given element is present in an ArrayList or not?

Using contains() method of ArrayList, we can examine whether the ArrayList contains the given element or not. This method returns true if ArrayList has that element otherwise returns false.


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

        list.add(1.1);

        list.add(11.11);

        list.add(111.111);

        list.add(1111.1111);

        //Checking whether list conatins '111.1111'

        System.out.println(list.contains(111.1111));    //Output : false
    }
}

No comments:

Post a Comment