A Developer Gateway To IT World...

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

Create an ArrayList and add some items in list and follow the following point: (a) remove an index value, (b) create a copy of all item as list2, (c) show an clone list, (d) check the size of list that should not be zero and then clear the list , (e) if list has elements then clear list and display after clear

Program code in each steps:

Use the following way to create list of arraylist:

ArrayList   mylist = new ArrayList();
       mylist.add(10);
       mylist.add(9);
       mylist.add("heera");


(a) remove an index value, 

mylist1.remove(2);


(b) create a copy of all item as list2, 


ArrayList   mylist1 = new ArrayList();
      mylist1.addAll(mylist);


(c) show an clone list, 



System.out.println("clone list is "+mylist.clone());

//[10, Tarun, 9, heera]



(d) check the size of list that should not be zero and then clear the list ,



 if(mylist.size()!=0)
 {
      mylist.clear();
      System.out.println("list has been cleaned.");
 }
 System.out.println("after claer list is "+mylist);



 (e) if list has elements then clear list and display after clear



if(mylist.size()!=0)
        {
            mylist.clear();
            System.out.println("list has been cleaned.");
        }
        System.out.println("after claer list is "+mylist);
       
        if(mylist.isEmpty())
        {
            System.out.println("List is empty.");
        }
        else
        {
            System.out.println("Removing all element..");
            mylist.clear();
        }




complete Java program code:



import java.util.ArrayList;
public class A
{
    public static void main(String a[])
    {
        ArrayList   mylist = new ArrayList();
        mylist.add(10);
        mylist.add(9);
        mylist.add("heera");
       
       
        ArrayList   mylist1 = new ArrayList();
        mylist1.addAll(mylist);
       
        System.out.println("mylist2 is "+mylist1);
        mylist1.remove(2);
        System.out.println("New mylist2 is "+mylist1);
       
       
       
        System.out.println("list is: "+mylist);// [10, 9, heera]
        mylist.add(1, "Tarun");
        System.out.println("new list is "+mylist);//[10, Tarun, 9, heera]
       
        System.out.println("clone list is "+mylist.clone());//[10, Tarun, 9, heera]
      
        if(mylist.contains("Tarun")==true)
        {
            System.out.println("Search found...");
        }
       
        System.out.println("Element at index 2: "+mylist.get(2));//9
       
        if(mylist.size()!=0)
        {
            mylist.clear();
            System.out.println("list has been cleaned.");
        }
        System.out.println("after claer list is "+mylist);
       
        if(mylist.isEmpty())
        {
            System.out.println("List is empty.");
        }
        else
        {
            System.out.println("Removing all element..");
            mylist.clear();
        }
    }
   
}


LEARN TUTORIALS

.

.