Hi,
I have just started peeping into jdk 1.5.
Generics were introduced so as to avoid the type casting for objects when retrieved from a collection. This feature is quite helpfull but i need some clarifications.
I have written a class as :
public class
Test {
public static void main(
String args[]) {
Test t = new Test();
ArrayList<String> a = new ArrayList();
a.add("a");
a.add("b");
a.add("c");
a.add("d");
a.add("e");
t.arrayListTest(a);
}
//******* generics in Collections **********//
public void arrayListTest(ArrayList a) {
for (int i=0; i < a.size();i++) {
String str1 = a.get(i);
System.out.println(str1);
}
}
}
When the
java class is compiled,
I recieve an error saying "Cannot convert from Object to String"
at
String str1 = a.get(i);
I would like to know why is the generic type set for ArrayList a in main() is not applicable inside arrayListTest() method.
Regards
P. Vamsi Mohan