dagblog.
← Archive

Programming puzzle — The enhanced for loop


If you don't know the Java 5.0 API and syntax, the following puzzle won't make sense to you. I pulled it out of an example that NkD and I were looking at and scratching our heads over. So here's the puzzle distilled to its purest form.

public static void main(String[] args) { 

 Set col = new HashSet();

 col.add("Dagi");

 col.add("NkD");

 for (String key : col) {

 col.remove(key);

 System.out.println(key); 

 }

 }

What's the output? Please include an explanation in the comments.

Medium version

Related articles

Related CZ Podcast episodes