Java几种集合的遍历方法

来源:互联网 发布:无限恐怖网络剧 编辑:程序博客网 时间:2024/06/05 07:39

(一):Map

long num1 = 100,num2 = 200;String str1="hello",str2="world";Map<Long, String> map = new HashMap<>();map.put(num1,str1);map.put(num2,str2);for(Map.Entry<Long, String> entry:map.entrySet()){    System.out.println("key= "+entry.getKey()+" value= "+entry.getValue());}

(二):List

List<String> list = new ArrayList<>();list.add("hello");list.add("world");1):for(Iterator it = list.iterator();it.hasNext();){    System.out.println(it.next());}2):for(String str:list){    System.out.println(str);}3):for(int i = 0;i<list.size();i++){    System.out.println(list.get(i));}

…更新中

0 0
原创粉丝点击