1)clone:This method is used to make a exact copy of list.This method returns object so we need to add casting to it.
Suppose list is an ArrayList and we have added some elements to it.
ArrayList<String> list=new ArrayList<String>();
list.add("First List");
list.add(1, "I");
list.add(0, "Abhishek");
So apply clone method on it
ArrayList<String> list1=new ArrayList<String>();
list1=(ArrayList<String>) list.clone();//to get the exact copy
2)clear():This method is used to clear the list means all the data present in the list will be lost and list will become empty.
list.clear();
3)contains():This method accepts object as argument to chech whether it is present in the list or not.It returns true if element is present else returns false.
boolean exist=list.contains("Abhishek");
4)containsAll():This method accepts collection as its argument and checks all the elements with the current collection to check whether every element in both of these collections are same or not.If every element is present then it returns true else returns false.
exist=list.containsAll(list1);
Note:The presence of elements of passed list is searched.
for example in above statement if list contains all the elements which are present in list1 then it will return true.It will not check the presence of all elements of list in list1.
The whole concept explained above is explained in the following program:
import java.util.ArrayList;
public class ArrayListOther {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<String>();
System.out.println("This is an example of add method in ArrayList");
list.add("First List");
System.out.println("After adding elements list is "+list );
list.add(1, "I");
System.out.println("After adding elements list is "+list );
list.add(0, "Abhishek");
System.out.println("After adding elements list is "+list );
ArrayList<String> list1=new ArrayList<String>();
list1=(ArrayList<String>) list.clone();
System.out.println("The cloned list of list is "+list1);
boolean exist=list.contains("Abhishek");
System.out.println("List contains element Abhishek: "+exist);
list.add("hello");
exist=list.containsAll(list1);
System.out.println("List contains all elements of other list : "+exist);
list.clear();
System.out.println("After removing all the elements using clear method list is"+list);
}
}
Output:
This is an example of add method in ArrayList
After adding elements list is [First List]
After adding elements list is [First List, I]
After adding elements list is [Abhishek, First List, I]
The cloned list of list is [Abhishek, First List, I]
List contains element Abhishek: true
List contains all elements of other list : true
After removing all the elements using clear method list is[]
So till now i have explained some methods of ArrayList class.Hope you are able to understand.If there is any problem then you can ask me by comment or email.
Thanks