遍历java集合

        // 遍历list
        Vector<String> v = new Vector<String>();
        v.add("张三");
        v.add("李四");
        Iterator<String> it = v.iterator();
        while (it.hasNext())
            System.out.println(it.next());

        for (String s : v) {
            System.out.println(s);
        }

        // 遍历map
        Map<String, String> map = new HashMap<String, String>();
        map.put("姓名", "张三");
        map.put("年龄", "20");
        for (String key : map.keySet()) {
            System.out.println(map.get(key));
        }
        for (HashMap.Entry<String, String> item : map.entrySet()) {
            System.out.println(item.getKey() + ":" + item.getValue());
        }

猜你喜欢

转载自www.cnblogs.com/yinchh/p/10409577.html