List集合输出3种方式,Set集合输出2种方式

2017-04-14  本文已影响0人  小沙鹰168
public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        list.add("zhaoliu");
        for (int i = 0; i < list.size(); i++) {
            String str = list.get(i);
            System.out.print(str + " ");
        }
        System.out.println();
        for (String str : list) {
            System.out.print(str + " ");
        }
        System.out.println();
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String str = it.next();
            System.out.print(str + " ");
        }

        Set<String> set = new HashSet<String>();
        set.add("zhangsan");
        set.add("lisi");
        set.add("wangwu");
        set.add("zhaoliu");
        System.out.println();
        for (String str : set) {
            System.out.print(str + " ");
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读