利用Entry接口,快速又方便

import java.util.HashMap;
import java.util.Map;

public class MapDemo {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1,"one");
map.put(2,"two");
map.put(3,"three");
for(Map.Entry<Integer, String> entry: map.entrySet()){
System.out.println(entry.getKey() + "---" + entry.getValue());
}

//以上的foreach类似下面这个
int[] num = new int[3];
for (int i = 0; i < 3; i++) {
num[i] = i;
}
for (int i : num){
System.out.println(i);
}
}
}

 

 posted on 2021-04-14 22:47  又在化学楼迷路了  阅读(489)  评论(0)    收藏  举报