zoukankan      html  css  js  c++  java
  • [Java] 容器-03 增强的For循环 / Set 方法

    import java.util.*;
    
    public class EnhancedFor {
        public static void main(String[] args) {
            int[] arr = {1, 2, 3, 4, 5};
            for(int i : arr) {
                System.out.println(i);
            }
            
            Collection c = new ArrayList();
            c.add(new String("aaa"));
            c.add(new String("bbb"));
            c.add(new String("ccc"));
            for(Object o : c) {
                System.out.println(o);
            }
        }
    }
    
    import java.util.*;
    // import java.util.Set;
    
    public class TestSet {
        public static void main(String[] args) {
            Set s1 = new HashSet();
            Set s2 = new HashSet();
            s1.add("a"); s1.add("b"); s1.add("c");
            s2.add("d"); s2.add("a"); s2.add("b");
            // Set 与 List 容器类都具有 Constructor(Collection c)
            // 构造方法用以初始化容器类
            Set sn = new HashSet(s1);
            sn.retainAll(s2); // 求交集
            Set su = new HashSet(s1);
            su.addAll(s2);
            System.out.println(sn);
            System.out.println(su);
        }
    }
    [b, a]
    [d, b, c, a]

  • 相关阅读:
    我来解数独(附delphi源码)
    jquery(三)
    jquery(二)
    jquery(一)
    前端之JS(五)
    前端之JS(四)
    前端之JS(三)
    前端之JS(二)
    前端之CSS(三)
    前端之CSS(二)
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786905.html
Copyright © 2011-2022 走看看