D
Dave Stallard
OK, I finally caught up to Java 1.5/6. (Delay had to do with available
tools and my intolerance for Eclipse, but I've reconciled myself to it now).
Question: Eclipse gives me a compiler error for:
List list = new ArrayList();
for (String s : list)
System.out.println(s);
It says "Type mismatch: Cannot convert from element type Object to
String". It's fine if I change decl of list to List<String>, or coerce
list to List<String> in the loop decl;
Is this an *actual* compiler error in the Java spec? Is Java actually
that hissy about the for-each variable? The compiler doesn't mind the
code below, even though it causes a runtime type-cast error. So why
can't it insert the cast to String in the loop setup? Am I missing
something?
Dave
List ar = new ArrayList();
ar.add(1);
List<String> list = ar;
for (String s : list)
Utils.log(s);
tools and my intolerance for Eclipse, but I've reconciled myself to it now).
Question: Eclipse gives me a compiler error for:
List list = new ArrayList();
for (String s : list)
System.out.println(s);
It says "Type mismatch: Cannot convert from element type Object to
String". It's fine if I change decl of list to List<String>, or coerce
list to List<String> in the loop decl;
Is this an *actual* compiler error in the Java spec? Is Java actually
that hissy about the for-each variable? The compiler doesn't mind the
code below, even though it causes a runtime type-cast error. So why
can't it insert the cast to String in the loop setup? Am I missing
something?
Dave
List ar = new ArrayList();
ar.add(1);
List<String> list = ar;
for (String s : list)
Utils.log(s);