Java ArrayList

The java collections framework is made available by the java.util package. Many useful interface, such as Collection, List and Set, are defined in this framework. Many useful classes, such as Collections, LinkedList, HashSet and Treeset are defined in the framework.

THE ARRAYLIST CLASS

The ArrayList class extends the AbstractList class, which is a predefined class. The ArrayList class implements the List interface. This class supports a dynamic array. As we know, a dynamic array can grow or shrink as needed. Actually, the ArrayList class deals with a variable-length array of object references.

The important forms of the constructor of the ArrayList class are as follows:

ArrayList( )
ArrayList(Collection c)
ArrayList(int capacity)

When we use the first form of the Constructor, an array list will be formed. The second form of the constructor initializes the elements of the newly created array list by using the elements of the collection C. When the third form of the constructor is used, the initial capacity of the array list is specified. The initial capacity grows or shrinks as needed.

The add() method is used to add objects to a collection.

The remove() method shall be used to remove an unwanted object from a collection.

Program

0

 

 

 

Program Source

import java.util.ArrayList;

public class Javaapp {

    public static void main(String[] args) {
        
        ArrayList array1 = new ArrayList();
        array1.add(10);
        array1.add(30);
        array1.add(40);
        array1.add(55);
        array1.add(60);
        
        System.out.println("array1 size  = "+array1.size());
        System.out.println("array1 datas = "+array1);
        
        array1.remove(3);
        array1.add(1,20);
        array1.add(4,50);
        
        System.out.println("array1 size  = "+array1.size());
        System.out.println("array1 datas = "+array1);
    }
}

Leave a Comment