Java:这个for循环有什么作用?

-4 java android

for (String key : output.keySet())
{
       // some code
}
Run Code Online (Sandbox Code Playgroud)

输出是hashmap的一个对象,我想遍历所有值的hashmap.我在互联网上找到了一个代表循环的例子......但是当我尝试执行它时,Eclipse不允许我这样做.请告诉我一个有效的方法或解释这个循环.这样我就可以遍历地图了.

我的函数伪有点像这样

public void function (String key)
{
     Set<String> keys = output.keySet();

    // now I have to iterate through the keySet and match 'key' with the keys from keySet 
}
Run Code Online (Sandbox Code Playgroud)

And*_*s_D 6

这是一段用Java 1.5+编译的代码.它使用泛型增强的for循环:

public static void main(String[] args) {
  Map<String, String> output = new HashMap<String, String>();
  output.put("1", "one");
  output.put("2", "two");
  output.put("3", "three");

  for (String key:output.keySet()) {
    System.out.println(output.get(key));
  }
}
Run Code Online (Sandbox Code Playgroud)

请将其与您自己的代码进行比较.你用"android"标记了你的问题,所以我希望你的目标是Java6源代码.如果仍然无效,请详细评论.