1) add(Object o):
al.add(0,"India");
//after removal
//before updating
//after updating
This method adds an
element to ArrayList.
Eg:
public static void
main(String[]args){
//creation of
Arraylist with string arguments
//adding elements to
Arraylist
ArrayList<String>
al=new ArrayList<String>();
al.add("India");
al.add("US");
al.add("Australia");
al.add("Germany");
al.add("Europe");
//Displaying elements
of Arraylist
System.out.println(al);
}
Output:[India,US,Australia,Germany,Europe]
2)add(int index,
object 0)
This method adds
object 0 to an ArrayList to at given index
Eg:
public static void
main(String[]args){
//creation of
Arraylist with string arguments
//adding elements to
Arraylist
ArrayList<String>
al=new ArrayList<String>();
al.add(1,"US");
al.add(2,"Australia");
al.add(3,"Germany");
al.add(4,"Europe");
//Displaying
elements of Arraylist
System.out.println(al);
}
Output:[India,US,Australia,Germany,Europe]
.
3)remove(int index)
Removes an element
from given index.
public static void
main(String[]args){
//creation of
Arraylist with string arguments
//adding elements to
Arraylist
ArrayList<String>
al=new ArrayList<String>();
al.add("India");
al.add("US");
al.add("Australia");
al.add("Germany");
al.add("Europe");
//Displaying elements
of Arraylist before removing elements of ArrayList
System.out.println(al);
//after removing
elements from ArrayList at specified index of ArrayList
al.remove(3);
//Displaying elements
of Arraylist
System.out.println(al);
}
//Before removal
Output[India,US,Australia,Germany,Europe]
//After removal
Output:[India,US,Australia,Europe]
4)remove(Object 0)
Removes an Object o
from ArrayList.
public static void
main(String[]args){
//creation of
Arraylist with string arguments
//adding elements to
Arraylist
ArrayList<String>
al=new ArrayList<String>();
al.add("India");
al.add("US");
al.add("Australia");
al.add("Germany");
al.add("Europe");
//Displaying elements
of Arraylist before removing elements of ArrayList
System.out.println(al);
//after removing
elements from ArrayList at specified index of ArrayList
al.remove("Germany");
//Displaying elements
of Arraylist
System.out.println(al);
}
Output[India,US,Australia,Germany,Europe]
//after removal
Output:[India,US,Australia,Europe]
5)set(int index,
Object o)
This method is used
for Updating an element which replaces element present at specified index with
object o.
public static void
main(String[]args){
//creation of
Arraylist with string arguments
//adding elements to
Arraylist
ArrayList<String>
al=new ArrayList<String>();
al.add("India");
al.add("US");
al.add("Australia");
al.add("Germany");
al.add("Europe");
//Displaying elements
of Arraylist before updating elements of ArrayList
System.out.println(al);
//after updating
elements from ArrayList at specified index of ArrayList
al.set(2,"London");
//Displaying elements
of Arraylist
System.out.println(al);
}
//before updating
Output[India,US,Australia,Germany,Europe]
//after updating
Output:[India,US,London,Germany,Europe]
6)int indexOf(Object o)
This method gives the
index of Object o. If the element is not found in the list it returns -1.
public static void
main(String[]args){
//creation of
Arraylist with string arguments
//adding elements to
Arraylist
ArrayList<String>
al=new ArrayList<String>();
al.add(0,"India");
al.add(1,"US");
al.add(2,"Australia");
al.add(3,"Germany");
al.add(4,"Europe");
//Displaying
elements of Arraylist
al.indexOf("Australia");
System.out.println(al);
}
Output:2
7)Object get(int index
)
It returns the object
of list which is present at specified index.
public static void
main(String[]args){
//creation of
Arraylist with string arguments
//adding elements to
Arraylist
ArrayList<String>
al=new ArrayList<String>();
al.add(0,"India");
al.add(1,"US");
al.add(2,"Australia");
al.add(3,"Germany");
al.add(4,"Europe");
//Displaying elements
of Arraylist
al.get(4);
System.out.println(al);
}
Output:Europe
8)int size()
It gives the size of
Arraylist.
public static void
main(String[]args){
//creation of
Arraylist with string arguments
//adding elements to
Arraylist
ArrayList<String>
al=new ArrayList<String>();
al.add(0,"India");
al.add(1,"US");
al.add(2,"Australia");
al.add(3,"Germany");
al.add(4,"Europe");
//Displaying size of
elements of Arraylist
al.size();
System.out.println(al);
}
Output: 5
9)boolean
contains(Object o)
It checks whether the
given object o is present in the arrayList .If the element is present it
returns true else returns false.
public static void
main(String[]args){
//creation of
Arraylist with string arguments
//adding elements to
Arraylist
ArrayList<String>
al=new ArrayList<String>();
al.add(0,"India");
al.add(1,"US");
al.add(2,"Australia");
al.add(3,"Germany");
al.add(4,"Europe");
//Displaying size of
elements of Arraylist
al.contains("India");
System.out.println(al);
}
Output: true
10)Clear
This method is used to
clear all the elements from Arraylist.
public static void
main(String[]args){
//creation of
Arraylist with string arguments
//adding elements to
Arraylist
ArrayList<String>
al=new ArrayList<String>();
al.add(0,"India");
al.add(1,"US");
al.add(2,"Australia");
al.add(3,"Germany");
al.add(4,"Europe");
//Displaying size of
elements of Arraylist
al.clear();
System.out.println(al);
}
Output: []
0 comments:
Post a Comment