Example Using ArrayList .


import java.util.ArrayList;

 class ArrayList
{
public static void main(String as[]){

ArrayList al = new ArrayList();
System.out.println(al); // []
System.out.println(al.toString()); // []
System.out.println(al.size()); // 0
System.out.println(al.isEmpty()); // true
al.add("ccc");
System.out.println(al.add("aaa")); // true
al.add("bbb");
al.add(new Integer(99));
al.add("bbb");
System.out.println(al); // [ccc, aaa, bbb, 99, bbb]
System.out.println(al.size()); // 5
System.out.println(al.isEmpty()); // false

Object obj[] = al.toArray();
for(int i=0;i<obj.length;i++)
System.out.print(obj[i] +"\t"); // ccc aaa bbb 99 bbb
System.out.println("");
System.out.println(al.contains("aaa")); // true
System.out.println(al.contains("aa")); // false


ArrayList al1 = new ArrayList(al);
System.out.println(al); // [ccc, aaa, bbb, 99, bbb]
System.out.println(al.toString()); // [ccc, aaa, bbb, 99, bbb]
System.out.println(al1); // [ccc, aaa, bbb, 99, bbb]
System.out.println(al1.toString()); // [ccc, aaa, bbb, 99, bbb]

System.out.println(al.equals(al1)); // true
System.out.println(al1.add("ddd")); // true
System.out.println(al1); // [ccc, aaa, bbb, 99, bbb, ddd]
System.out.println(al.equals(al1)); // false
al1.remove("ddd");
System.out.println(al1); // [ccc, aaa, bbb, 99, bbb]


ArrayList al2 = new ArrayList();
al2.add("111");
System.out.println(al2.add("222")); // true
al2.add("333");
al2.add("444");
System.out.println(al); // [ccc, aaa, bbb, 99, bbb]
System.out.println(al1); // [ccc, aaa, bbb, 99, bbb]
System.out.println(al2); // [111, 222, 333, 444]
al.add(al2);
System.out.println(al); // [ccc, aaa, bbb, 99, bbb, [111, 222, 333, 444]]
al1.addAll(al2);
System.out.println(al1); // [ccc, aaa, bbb, 99, bbb, 111, 222, 333, 444]

System.out.println(al.contains(al2)); // true
System.out.println(al.containsAll(al2)); // false
System.out.println(al1.contains(al2)); // false
System.out.println(al1.containsAll(al2)); // true
System.out.println(al1); // [ccc, aaa, bbb, 99, bbb, 111, 222, 333, 444]
al1.retainAll(al2);
System.out.println(al1); // [111, 222, 333, 444]


System.out.println("Using Iterator:"); // Using Iterator:
Iterator it = al.iterator();
while(it.hasNext()){
 System.out.print(it.next()+"\t"); // ccc  aaa  bbb  99  bbb  [111, 222, 333, 444]
}
 System.out.println("");

System.out.println(al2); // [111, 222, 333, 444]
al2.clear();
System.out.println(al2); // []
}
 }

0 comments :

Post a Comment