The best place to *find* answers to programming/development questions, imo, however it's the *worst* place to *ask* questions (if your first question/comment doesn't get any up-rating/response, then u can't ask anymore questions--ridiculously unrealistic), but again, a great reference for *finding* answers.

My Music (Nickleus)

20121031

enhanced for loop (for each) for java.util.collection - Type mismatch: cannot convert from element type Object to String

i had the following code that i wanted to convert to the new enhanced for loop:
private String myMethod(Collection c){
    for(Iterator<String> it = c.iterator(); it.hasNext();){
        String s = it.next();
        ...
    }
}





so i rewrote it like this:
private String myMethod(Collection c){
    for(String s : c){
        ...
    }
}

but that code gave me the following "error" in eclipse:
Type mismatch: cannot convert from element type Object to String



here's the final, error-free solution:
private String myMethod(Collection<String> c){
    for(String s : c){
        ...
    }
}

No comments:

Post a Comment