package com.myapp.test;
import java.util.ArrayList;
import java.util.Iterator;
public class GenericTest {
public static void main(String[] args) {
// OLD Style
ArrayList oldStyle = new ArrayList();
oldStyle.add(new String("Hello"));
oldStyle.add(new String("there"));
oldStyle.add(new Integer(12)); // ok the old way
oldStyle.add(new String("whoops"));
// following loop raise runtime error
for (Iterator i = oldStyle.iterator(); i.hasNext();) {
System.out.println("Entry= " + (String) i.next());
}
// New Style
ArrayList<String> newStyle = new ArrayList<String>();
newStyle.add(new String("Hello"));
newStyle.add(new String("there"));
// following line raises compile error so it is commented/
newStyle.add(new Integer(12));
// compile error
newStyle.add(new String("whoops"));
for (Iterator<String> i = newStyle.iterator(); i.hasNext();) {
System.out.println("Entry= " + i.next());
}
}
}
import java.util.ArrayList;
import java.util.Iterator;
public class GenericTest {
public static void main(String[] args) {
// OLD Style
ArrayList oldStyle = new ArrayList();
oldStyle.add(new String("Hello"));
oldStyle.add(new String("there"));
oldStyle.add(new Integer(12)); // ok the old way
oldStyle.add(new String("whoops"));
// following loop raise runtime error
for (Iterator i = oldStyle.iterator(); i.hasNext();) {
System.out.println("Entry= " + (String) i.next());
}
// New Style
ArrayList<String> newStyle = new ArrayList<String>();
newStyle.add(new String("Hello"));
newStyle.add(new String("there"));
// following line raises compile error so it is commented/
newStyle.add(new Integer(12));
// compile error
newStyle.add(new String("whoops"));
for (Iterator<String> i = newStyle.iterator(); i.hasNext();) {
System.out.println("Entry= " + i.next());
}
}
}