How ArrayList Class In Java Collections Are Used

Introduction

This article describes how the ArrayList class in Java Collections work.

ArrayList class

What does it mean?

Java arrays are of a fixed length. When arrays are created, they are unable to grow or shrink, in other words we must know in advance how many elements we need so that the array will hold them. Now, the ArrayList class extends Array-List and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.

This class supports mainly three constructors.

ArrayList(int capacity)
Apart from the methods inherited from its parent classes, ArrayList defines the following methods.
ArrayList( )
This constructor builds an array list that is initialized with the elements of the collection cln.
ArrayList(Collection cln)
This constructor builds an array list that has the specified initial capacity. The capacity grows automatically as elements are added to an array list.

Example

The following example contains various methods used by the ArrayList class.

import java.util.*;

public class ArrayListEx1
 
{

    public static void main(String args[])

      {

        // create an list of array

        ArrayList arylst = new ArrayList();

        System.out.println("size of initialized arraylist: " + arylst.size());

        // now, add elements to the arraylist

        arylst.add("C");

        arylst.add("P");

        arylst.add("Q");

        arylst.add("R");

        arylst.add("S");

        arylst.add("T");

        arylst.add(1, "P2");

        System.out.println("Size of arraylist after additions: " + arylst.size());

        // display the array list

        System.out.println("Contents of arraylist: " + arylst);

        // Remove elements from the array list

        arylst.remove("T");

        arylst.remove(2);

        System.out.println("After deletion size of arraylist: " + arylst.size());

        System.out.println("The contents of arraylist: " + arylst);

      }

  }

Output

Fig-1.jpg

The following are two ways to iterate the elements of the collection:

  1. By Iterator interface.
  2. By For-each loop

1. By using For-each loop

Example

import java.util.*;

class ArrayListEx2

  {

    public static void main(String args[])

      {

        ArrayList arylst=new ArrayList();

        arylst.add("John");

        arylst.add("Paul");

        arylst.add("Jaun");

        arylst.add("San");

        for (Object obj:arylst)

        System.out.println(obj);

      }

  }

Output

Fig-2.jpg

2. By using Iterator interface

Example

import java.util.*;

class ArrayListEx4

  {

    public static void main(String args[])

      {

        ArrayList arylst=new ArrayList();

        arylst.add("Paul");

        arylst.add("John");

        arylst.add("Paul");

        arylst.add("San");

        Iterator itrtr=arylst.iterator();

        while(itrtr.hasNext())

          {

            System.out.println(itrtr.next());

          }

      }

  }

Output

Fig-4.jpg

Storing user-defined class objects

Chilgren.java

This class contains Student records, such as their name, age and rollno.

class Children

  {

    int rollno;

    String name;

    int age;

    Children (int rollno, String name, int age)

      {

        this.rollno=rollno;

        this.name=name;

        this.age=age;

      }

  }

ArrayListEx3.java

This class is used to show or print the records of the Children defined class.

import java.util.*;

class ArrayListEx3

  {

    public static void main(String args[])

      {

        Children c1= new Children(11, "Paul", 21);

        Children c2= new Children(12, "Sam", 23);

        Children c3= new Children(13, "Cady", 22);

        ArrayList arylst=new ArrayList();

        arylst.add(c1);

        arylst.add(c2);

        arylst.add(c3);

        Iterator it=arylst.iterator();

        while(it.hasNext())

          {

            Children ch=(Children)it.next();

            System.out.println(ch.rollno+"  "+ch.name+"  "+ch.age);

          }

      }

  }


Output

fig-3.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all