A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Example of ArrayList and its method in collection

//import packages 
import java.util.ArrayList;
import java.util.function.Consumer;
public class Coll
{

    public static void main(String[] args)
    {
       
         //obj AL
        ArrayList list = new ArrayList();

        list.add(10);
        list.add(11);
        list.add(13);
        list.add(14);
        list.add(16);
        list.add(20);
        list.add(22);
       
        //find index by value
        System.out.println(list.indexOf(13));

        System.out.println("New list: "+list);
       
        //create same type of list by clone() method
        System.out.println("clone list: "+list.clone());
       

        //add element at created index     
        list.add(3, 33);
        System.out.println(list);

        //check list empty of full
        System.out.println(list.containsAll(list));
       
        //elements in list
        System.out.println(list);
       
        //element at index
        System.out.println(list.get(3));
       
        //remove element at index
        list.remove(3);
        System.out.println(list);
       
        ArrayList list1 = new ArrayList();
       
        //same type of list
        list1.addAll(list);
        System.out.println(list1);
       
        //clear element from ArrayLIst
        list.clear();
        System.out.println(list);

       
       
    }

}


Sample output:

2
New list: [10, 11, 13, 14, 16, 20, 22]
clone list: [10, 11, 13, 14, 16, 20, 22]
[10, 11, 13, 33, 14, 16, 20, 22]
true
[10, 11, 13, 33, 14, 16, 20, 22]
33
[10, 11, 13, 14, 16, 20, 22]
[10, 11, 13, 14, 16, 20, 22]
[ ]

LEARN TUTORIALS

.

.