×
Table of Contents
Iterator without Generics Example
Overview
- Note: Using Raw Type Member of Collections may throw an compiler Warning
- Workaround: Use Genrics
Java Code
import java.util.ArrayList; import java.util.Iterator; public class IteratorDemo1 { public static void main(String args[]){ ArrayList names = new ArrayList(); names.add("Chaitanya"); names.add("Steve"); names.add("Jack"); Iterator it = names.iterator(); while(it.hasNext()) { String obj = (String)it.next(); System.out.println(obj); } } }
Compiler Warnings
D:\JAVA\tutorial\Iterator> javac IteratorDemo1.java Note: IteratorDemo1.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. D:\JAVA\tutorial\Iterator> javac -Xlint:unchecked IteratorDemo1.java IteratorDemo1.java:8: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList names.add("Chaitanya"); ^ where E is a type-variable: E extends Object declared in class ArrayList IteratorDemo1.java:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList names.add("Steve"); ^ where E is a type-variable: E extends Object declared in class ArrayList IteratorDemo1.java:10: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList names.add("Jack"); ^ where E is a type-variable: E extends Object declared in class ArrayList 3 warnings
Output
D:\JAVA\tutorial\Iterator> java IteratorDemo1 Chaitanya Steve Jack
Iterator Example throwing a ClassCastException during Runtine
- IteratorDemo2.java throws a Runtime Exception java.lang.ClassCastException – this should be avoided !
Java Code
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorDemo2 {
public static void main(String args[]){
ArrayList names = new ArrayList();
names.add("Chaitanya");
names.add("Steve");
names.add("Jack");
//Adding Integer value to String ArrayList
names.add(new Integer(10));
Iterator it = names.iterator();
while(it.hasNext()) {
String obj = (String)it.next();
System.out.println(obj);
}
}
}
Compiler Warnings
D:\JAVA\tutorial\Iterator> javac IteratorDemo2.java Note: IteratorDemo2.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
Output
D:\JAVA\tutorial\Iterator> java IteratorDemo2
Chaitanya
Steve
Jack
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at IteratorDemo2.main(IteratorDemo2.java:18)
Iterator sample using Generics throwing a Compile Error
- This sample throws Compiler Error: no suitable method found for add(Integer)
Java Code
import java.util.ArrayList; import java.util.Iterator; public class IteratorDemo4 { public static void main(String args[]){ ArrayListnames = new ArrayList (); names.add("Chaitanya"); names.add("Steve"); names.add("Jack"); names.add(new Integer(10)); Iterator it = names.iterator(); while(it.hasNext()) { String obj = it.next(); System.out.println(obj); } } }
Compiler Error
D:\JAVA\tutorial\Iterator> javac IteratorDemo4.java IteratorDemo4.java:10: error: no suitable method found for add(Integer) names.add(new Integer(10)); ^ method Collection.add(String) is not applicable (argument mismatch; Integer cannot be converted to String) method List.add(String) is not applicable (argument mismatch; Integer cannot be converted to String) method AbstractCollection.add(String) is not applicable (argument mismatch; Integer cannot be converted to String) method AbstractList.add(String) is not applicable (argument mismatch; Integer cannot be converted to String) method ArrayList.add(String) is not applicable (argument mismatch; Integer cannot be converted to String) Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error
Fix Compiler Error and run Sample
Java Code
import java.util.ArrayList; import java.util.Iterator; public class IteratorDemo3 { public static void main(String args[]){ ArrayListnames = new ArrayList (); names.add("Chaitanya"); names.add("Steve"); names.add("Jack"); Iterator it = names.iterator(); while(it.hasNext()) { String obj = it.next(); System.out.println(obj); } } }
No Compiler Warnings/errors
D:\JAVA\tutorial\Iterator> javac IteratorDemo3.java
Output
D:\JAVA\tutorial\Iterator> java IteratorDemo3 Chaitanya Steve Jack