import java.util.*;
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("b");s2.add("d");s2.add("c");
Set s3 = new HashSet(s1);
s3.retainAll(s2);
Set s4 = new HashSet(s1);
s4.addAll(s2);
System.out.println(s3);
System.out.println(s4);
}
}
class HashSet
{
HashSet(){}
}

解决方案 »

  1.   

    import java.util.*;
    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("b");s2.add("d");s2.add("c");
            Set s3 = new HashSet(s1);
            s3.retainAll(s2);
            Set s4 = new HashSet(s1);
            s4.addAll(s2);
            System.out.println(s3);
            System.out.println(s4);    
        }
    }
      

  2.   

    class HashSet { HashSet(){} }这个类和java.util.HashSet名字重复,分不清new HashSet();到底是new哪一个类了,加上包名来限定
    new java.util.HashSet();
      

  3.   

    相当正确!import java.util.*;
    public class TestSet
    {
        public static void main(String[] args)
        {
            Set s1 = new  java.util.HashSet();
            Set s2 = new java.util.HashSet();
            s1.add("a");s1.add("b");s1.add("c");
            s2.add("b");s2.add("d");s2.add("c");
            Set s3 = new java.util.HashSet(s1);
            s3.retainAll(s2);
            Set s4 = new java.util.HashSet(s1);
            s4.addAll(s2);
            System.out.println(s3);
            System.out.println(s4);    
        }
    }
    class HashSet
    {
        HashSet(){}
    }
    输出为:
    C:\>java TestSet
    [c, b]
    [d, a, c, b]
      

  4.   

    去掉class HashSet
    {
        HashSet(){}
    }
    就能编译了
      

  5.   

    不用再搞个HashSet类了吧。util包里已经提供了这个类了。直接用
      

  6.   


    这个程序中虽然定义了自己的HashSet但是类加载器并没有加载这个类。所以不报错。
    只要你使用自己的HashSet类必然会错。因为自己定义的HashSet不能向上塑型成Set.
      

  7.   


    不是分不清吧,按照楼主的写法new HashSet();一句中new 的就是自己定义的HashSet,这应该是个转型错误,自己的HashSet没办法转型成Set造成的。