关于list,map,set的区别参考http://www.cnblogs.com/qlqwjy/p/7406573.html
1.遍历list
@Test public void testList() { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); System.out.println("-------------------list------------------"); System.out.println("-------增强for循环list----"); for (int i : list) { System.out.println(i); } System.out.println("-------for循环list----"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println("-------迭代器遍历list----"); Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } }
结果:
-------------------list------------------ -------增强for循环list---- 1 2 -------for循环list---- 1 2 -------迭代器遍历list---- 1 2
2.遍历Map
@Test public void testMap() { Map<Integer, String> m = new HashMap<Integer, String>(); m.put(1, "s"); m.put(2, "s"); m.put(3, "s"); m.put(4, "s"); System.out.println("-------------------map------------------"); System.out.println("--------循环遍历键取值---------"); for (Map.Entry<Integer, String> entry : m.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } System.out.println("--------根据键取值(键正好为整数)---------"); for (int i = 0; i < m.size(); i++) { System.out.println(m.get(i + 1)); } }
-------------------map------------------ --------循环遍历键取值--------- 1 s 2 s 3 s 4 s --------根据键取值(键正好为整数)--------- s s s s
3.遍历Set
@Test public void testSet() { Set<Integer> set = new HashSet(); set.add(1); set.add(2); System.out.println("-------------------set------------------"); System.out.println("-------增强for循环----"); for (int i : set) { System.out.println(i); } System.out.println("-------迭代器遍历----"); Iterator<Integer> iterator = set.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } }
-------------------set------------------ -------增强for循环---- 1 2 -------迭代器遍历---- 1 2
4.遍历vector
@Test public void testVector() { System.out.println("-------------------vector------------------"); Vector<String> v = new Vector<String>(); v.add("sss"); v.add("sssss"); System.out.println("-------普通for循环----"); for (int i = 0; i < v.size(); i++) { System.out.println(v.get(i)); } System.out.println("-------增强for循环----"); for (String x : v) { System.out.println(x); } System.out.println("-------迭代器遍历----"); Iterator<String> iterator = v.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } }
-------------------vector------------------ -------普通for循环---- sss sssss -------增强for循环---- sss sssss -------迭代器遍历---- sss sssss
时间: 2024-10-13 14:47:42