考你一个小问题:"1000000.0"是一个什么类型的常量?备选答案:(a)String,(b)float

解决方案 »

  1.   

    由于你给的程序不全,只能对Test.main方法进行说明如下:
    Car的构造方法:Car(float,String),而你调用的是Car(String,String),
    所以new Car("1000000.0", "Benz")会出错。
    改为:new Car(1000000.0f, "Benz")就可以了(注意有小数点的默认为double类型,所以后面要加上f)
      

  2.   

    // 这样就行了,编译没问题.
    // 1000000.0 应该用double了.import java.util.*;class Car
    {
        double price;
        String brand;
        Car(double price, String brand)
        {
            this.price = price;
            this.brand = brand;
        }
        /*
        public boolean equals(Object o){ //....}
        public int hashCode(){//...}
        */
    }public class Test
    {
        public static void main(String[] args)
        {
            Set s = new TreeSet();
            
            s.add(new Car(1000000.0, "Benz"));
            s.add(new Car(2888888.88, "Bently"));
            System.out.println(s);
        }
    }
      

  3.   

    你的car类的构造函数的参数是float
    而调用时用的是"1000000.0" 这是string型,肯定是不对的。
      

  4.   

    上面有2句是我打字时疏忽了,应该是:
    s.add(new Car(1000000.0, "Benz"));
    s.add(new Car(2888888.88, "Bently"));
    可是这样有错,当我注释掉上面任何一句时,程序就可以正常运行了.这是为什么呢?
    望大虾们帮忙看看,小弟先谢谢了!
      

  5.   

    看我上面贴的代码了吗?
    price 用 double 就行了.  :)
      

  6.   

    注意有小数点的默认为double类型,所以后面要加上f.即把1000000.0和2888888.8后加f1000000.0f,2888888.88f就可以了
      

  7.   

    加上f是什么意思   函数原型要的是double类型的   不用加  默认就是double类型的
      

  8.   

    你可以在你的程序上加上这样一段代码
     public int compareTo(Object o) {
         return X;
        }
    X--> 如果X=0,就在treeset中添加一个object,如果是其他的值就可以添加好多
    你也可以看看我写好的code
    import java.util.*;public class TestTreeSet  implements Comparable
    {
        private double price;
        private String brand;
       TestEncoding(double price, String brand){
            this.price = price;
            this.brand = brand;
        }
        public static void main(String[] args)
        {
            Set s = new TreeSet();
            s.add(new TestTreeSet(1000000.0, "Benz"));
            s.add(new TestTreeSet(2888888.88, "Bently"));
            System.out.println(s.size());
        }
        public int compareTo(Object o) {
         return 3;
        }
    }
    这个问题涉及到了数据结构的黑白树的概念
      

  9.   

    建议你仔细阅读JDK API关于TREESET的说明,给你摘下面一段:
    Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.注意里面的这句:but a TreeSet instance performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the set, equal.
    这就是你出错的原因,你使用了TREESET,但是没有正确得重写compareTo方法。
      

  10.   

    谢谢各位了,尤其是:biti_9512207(波波斯基)和swinging(山不在高)2位兄弟,谢谢你们!