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

public static void main(String[] args) {
        List list = new ArrayList();
        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 it = list.iterator();
        while (it.hasNext()) {
            String str = it.next();
            System.out.print(str + " ");
        }

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

你可能感兴趣的:(List集合输出3种方式,Set集合输出2种方式)