高级for循环 实例

来源:互联网 发布:php放到path 环境变量 编辑:程序博客网 时间:2024/05/21 08:49

一 ArrayList

int array[] = {1,2,3,4};

for(int i : array){

    System.out.println(i);

}

二 List

List<String> list = new ArrayList<String>();

list.add("a");

list.add("b");

list.add("c");

for(String str : list){

    System.out.println(str);

}

三 HashMap

//第一种, 通过forpublic <K,V> V getValueByFor(Map<K,V> map){V value = null;map = new HashMap<K,V>();for(Map.Entry<K, V> mapEntry : map.entrySet()){value = mapEntry.getValue();}return value;}//第二种,通过迭代public <K,V> V getValueByIterator(Map<K,V> map){V value = null;map = new HashMap<K,V>();Iterator<Map.Entry<K,V>> iterator = map.entrySet().iterator();while(iterator.hasNext()){Map.Entry<K,V> mapEntry = iterator.next();value = mapEntry.getValue();}return value;}


0 0
原创粉丝点击