package com.listsample;
import java.util.*;
public class ListExample {
public static void main(String args[]) {
// Creating an empty array list
List list = new ArrayList();
// Adding items to arrayList - adding both integer and string values
list.add(2);
list.add(3);
list.add(6);
list.add("DDD");
// Display the contents of the array list
System.out.println("The arraylist contains the following elements: "
+ list);
/*
for (int i = 0; i < list.size(); i++) {
//This will throwClassCastingException
Integer iVal = Integer.valueOf(list.get(i).toString());
System.out.println("Index: " + i + " - Item: " + iVal);
}
// hasNext(): returns true if there are more elements
// next(): returns the next element
System.out.println("Retrieving items using iterator");
Iterator itr = list.iterator();
while (itr.hasNext()) {
String str = itr.next().toString();
System.out.println(str);
if (str.equals("AAA"))
itr.remove();
}
*/
// Generics - to restrict the collection object to specific data type
ArrayList<String> list1 = new ArrayList<String>();
list1.add("aaaa");
list1.add("2");
System.out.println("Availabiliy of the element " +list1.contains("bbb"));
ArrayList<String> list2 = new ArrayList<String>();
System.out.println("Before adding theelemets : "+list2.isEmpty());
list2.add("bbbb");
list2.add("aaaa");
list2.add("cccc");
list2.add("2");
list2.add("cccc");
//tostring()is to display the elements as string with comma separated values
System.out.println("String list "+list2.toString());
System.out.println("Position of the first occurence of the element "+list2.indexOf("cccc")+" the position of the duplicate "+list2.lastIndexOf("cccc"));
System.out.println("After adding the elements: "+list2.isEmpty());
System.out.println("Comparing the lists: "+list1.containsAll(list2));
Iterator<String> itr1 = list1.iterator();
while (itr1.hasNext()) {
String s = itr1.next();
System.out.println(s);
}
}
}
import java.util.*;
public class ListExample {
public static void main(String args[]) {
// Creating an empty array list
List list = new ArrayList();
// Adding items to arrayList - adding both integer and string values
list.add(2);
list.add(3);
list.add(6);
list.add("DDD");
// Display the contents of the array list
System.out.println("The arraylist contains the following elements: "
+ list);
/*
for (int i = 0; i < list.size(); i++) {
//This will throwClassCastingException
Integer iVal = Integer.valueOf(list.get(i).toString());
System.out.println("Index: " + i + " - Item: " + iVal);
}
// hasNext(): returns true if there are more elements
// next(): returns the next element
System.out.println("Retrieving items using iterator");
Iterator itr = list.iterator();
while (itr.hasNext()) {
String str = itr.next().toString();
System.out.println(str);
if (str.equals("AAA"))
itr.remove();
}
*/
// Generics - to restrict the collection object to specific data type
ArrayList<String> list1 = new ArrayList<String>();
list1.add("aaaa");
list1.add("2");
System.out.println("Availabiliy of the element " +list1.contains("bbb"));
ArrayList<String> list2 = new ArrayList<String>();
System.out.println("Before adding theelemets : "+list2.isEmpty());
list2.add("bbbb");
list2.add("aaaa");
list2.add("cccc");
list2.add("2");
list2.add("cccc");
//tostring()is to display the elements as string with comma separated values
System.out.println("String list "+list2.toString());
System.out.println("Position of the first occurence of the element "+list2.indexOf("cccc")+" the position of the duplicate "+list2.lastIndexOf("cccc"));
System.out.println("After adding the elements: "+list2.isEmpty());
System.out.println("Comparing the lists: "+list1.containsAll(list2));
Iterator<String> itr1 = list1.iterator();
while (itr1.hasNext()) {
String s = itr1.next();
System.out.println(s);
}
}
}
No comments:
Post a Comment