标签:maps 方法总结 增强for循环 key man set nbsp iterator value
遍历list方法:
1.普通for循环:for (int i = 0; i < list.length; i++){
String temp = (String)list.get(i);
System.out.println(temp);
}
2.增强for循环:for (String temp:list) {
System.out.println(temp); }
3.使用迭代器:for (Iterator iter = list.iterator();iter.hasNext();) {
String temp = (String) iter.next();
System.out.println(temp); }
或 Iterator iter = list.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
iter.remove();//遍历时,删除集合中的元素
System.out.println(obj); }
遍历set方法:1.增强for循环:for (String temp:set) {
System.out.println(temp); }
2.使用迭代器:for (Iterator iter = set.iterator();iter.hasNext();)
String temp = (String)iter.next();
System.out.println(temp); }
遍历map方法:1.根据key获得value: Map<Integer, man> maps = new HashMap<Integer, man>();
Set<Integer> keySet = maps.keySet();
for (Integer i :keySet) {
System.out.println(maps.get(i).name); }
2.使用entrySet: Set<Entry<Integer,man>> ss = maps.entrySet();
for (Iterator iter = ss.iterator();iter.hasNext();) {
Entry e = (Entry)iter.next();
System.out.println(e.getKey()+":"+e.getValue()); }
标签:maps 方法总结 增强for循环 key man set nbsp iterator value
原文地址:https://www.cnblogs.com/LuJunlong/p/11650171.html