ArrayList implements all List operations using List interface and is also accept null values.The ArrayList is re sizable i.e shrinks and grows automatically and it has a capacity .
The class hierarchy is as follow:
---->Java.Lang.Object
----->Java.Util.AbstactCollection
----->Java.Util.AbstractList
------>Java.Util.ArrayList
The array list contains following methods for implementing different operations
| Return type | Method name |
|---|---|
| void | add (int index, Object o) |
| boolean | add (Object o) |
| boolean | addAll (Collection c) |
| boolean | addAll (int index,Collection c) |
| void | clear ( ) |
| Object | clone ( ) |
| boolean | contains (Object o ) |
| Object | get (int index) |
| int | indexOf(Object o) |
| boolean | isEmpty( ) |
| int | lastIndexOf( Object o) |
| Object | remove( int index) |
| Protected void | removeRange( int fromIndex, int toIndex) |
| Object | set (int index, Object o) |
| int | size ( ) |
| Object [ ] | toArray( ) |
| Object [ ] | toArray (Object [ ] a) |
| void | trimToSize( ) |
The program bellow consists of above methods in a nutshell
package Object;
import java.util.ArrayList;
import java.util.ListIterator;
public class ArrayList1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> myList = new ArrayList<String>();
ArrayList<String>myList1 = new ArrayList<String>();
// adding elements
myList.add("a"); // add by object
myList.add("b");
myList.add("c");
myList.add("d");
myList.add("e");
myList.add("f");
myList.add("g");
myList.add(7, "k"); // add by index
myList1.addAll(myList ); // adding all elements of myList to myList1
System.out.println(myList);
System.out.println(myList1);
// inserting a element at index 2
myList.add(2, "element");
System.out.println(myList);
// set the in particular index
myList.set(0, "new set"); // setting "new set" at index 0
// checking the size
for (int i=0;i<myList.size();i++)
{
System.out.println("the element of index "+" " + i + " "+"is " + myList.get(i));
}
//remove by index
myList.remove(5);
System.out.println(myList); // e will be removed
//remove by object value
myList.remove("g");
System.out.println(myList); // g will be removed
//clone the array list
ArrayList<String>Testlist = (ArrayList<String>)myList.clone();
//shallow copy of this ArrayList instance prints
//[a, b, element, c, d, f, k]
System.out.println("the clone is " + Testlist);
// contains
System.out.println(myList.contains("a")); // returns false
System.out.println(myList.contains(Testlist)); // return false
// is empty
System.out.println(myList.isEmpty());
// lastIndexOf
System.out.println(myList.lastIndexOf("k")); // prints 6
System.out.println(myList.lastIndexOf("g")); //prints -1 because missing in list
//toArray
String [] myString = Testlist.toArray(new String[Testlist.size()]);
System.out.println(myString.toString()); //[Ljava.lang.String;@10385c1
System.out.println(Testlist.size());
//TrimToSize
Testlist.trimToSize();
System.out.println(Testlist);
//retainAll
System.out.println(myList1);
System.out.println(myList);
//retains all the elements which are common in in myList1 and myList
boolean b =myList1.retainAll(myList);
System.out.println(b);
System.out.println(myList1);
//ListIterator
System.out.println(myList1);
ListIterator<String> it = myList1.listIterator();
while(it.hasNext())
System.out.println(it.next()); // prints one by one elements in myList1
//remove, remove all
Testlist.remove(1);
System.out.println(Testlist); // b is removed
Testlist.removeAll(Testlist);
System.out.println(Testlist); // removes all element in Testlist, prints []
// clear the list
myList.clear();
System.out.println(myList); // prints []
}
}
//output are as follow:--
========================
[a, b, c, d, e, f, g, k] [a, b, c, d, e, f, g, k] [a, b, element, c, d, e, f, g, k] the element of index 0 is new set the element of index 1 is b the element of index 2 is element the element of index 3 is c the element of index 4 is d the element of index 5 is e the element of index 6 is f the element of index 7 is g the element of index 8 is k [new set, b, element, c, d, f, g, k] [new set, b, element, c, d, f, k] the clone is [new set, b, element, c, d, f, k] false false false 6 -1 [Ljava.lang.String;@30c221 7 [new set, b, element, c, d, f, k] [a, b, c, d, e, f, g, k] [new set, b, element, c, d, f, k] true [b, c, d, f, k] [b, c, d, f, k] b c d f k [new set, element, c, d, f, k] [] []
No comments:
Post a Comment