简体   繁体   English

内部类中的比较器(int)

[英]Comparator (int) in inner class

I have some problem with comparator. 我有比较器的问题。 I make very simple class with 2 fields and i need make inner classes with comparators for this two fields. 我使用2个字段制作了非常简单的类,并且需要使用这两个字段的比较器创建内部类。

public class KlassA {

    private int i;
    private String tekst;  

    public int getI(){
        return i;

This comparator works ok. 该比较器工作正常。

   public static class KomparatorText implements Comparator<KlassA>{
        public int compare(KlassA a1, KlassA a2) {
            return a1.getTekst().compareTo(a2.getTekst());
        }

   }

In this comparator i have some issue: "Cannot invoke compareTo(int) on the primitive type int" 在此比较器中,我遇到一些问题:“无法在原始类型int上调用compareTo(int)”

   public static class KomparatorI implements Comparator<KlassA>{
        public int compare(KlassA a1, KlassA a2) {
            return a1.getI().compareTo(a2.getI());
        }
   }

I also try CompareTo but then i need to implements field or method and it's not work 我也尝试了CompareTo,但是然后我需要实现字段或方法,但是它不起作用

   public static class KomparatorI implements Comparable<KlassA>{

        private int i = this.i;

        public int compareTo(KlassA o) {
           if (this.i < o.i)
              return -1;
           else if (this.getI() == o.getI())
              return 0;
           else
              return 1;
       }
   }
}

I really don't know how to fix it. 我真的不知道该如何解决。 I'm already serach the other inquiries but all sollutions don't work for my example. 我已经搜索了其他查询,但是所有解决方案均不适用于我的示例。 Pls help. 请帮助。

You could force everything to Integers as in the other answers, but, IMO, better to keep your current class structure with ints and to use a built in method, Integer.compare() 您可以像其他答案一样将所有内容强制给Integers,但是,IMO最好将当前的类结构保留为ints并使用内置方法Integer.compare()

public static class KomparatorI implements Comparator<KlassA>{
   public int compare(KlassA a1, KlassA a2) {
      return Integer.compare(a1.getI(), a2.getI());
   }
}

Likewise, if you wish to use Comparable instead, use it to save typing (and avoid mistakes in the logic): 同样,如果您希望改用Comparable,请使用它来保存类型(并避免逻辑错误):

public static class KomparatorI implements Comparable<KlassA>{
   public int compareTo(KlassA o) {
      return Integer.compare(this.i, o.i);
   }
}

Lets see what you gout here 让我们看看你在这里痛风

a1.getI().compareTo(a2.getI());

it looks like getI() returns int insteed of Integer . 看起来getI()返回的是Integer int insteed。

Change mehod definition to return Integer insteed and all wil be fine. 更改方法定义以返回Integer insteed,一切都会好的。 So inside your KlaasA 因此,在您的KlaasA

public Integer getI(){ return i};

In Java, primitive types like int , float , or boolean are not objects like the correspondent Integer , Float , or Boolean .This means you cannot call methods on them. 在Java中,像intfloatboolean类的原始类型不是对应的IntegerFloatBoolean类的对象,这意味着您无法在它们上调用方法。

Try using, 尝试使用

Integer.valueOf(this.i).compareTo(o.i);

Your confusion probably comes from the fact that the compiler tries to covert primitive types to their corresponding object classes automatically. 您的困惑可能是由于编译器试图自动将原始类型隐式转换为它们对应的对象类。 In the sample above this happens for the argument oi , which is converted automatically to an Integer . 在上面的示例中,这发生在参数oi ,该参数自动转换为Integer Try searching for "java autoboxing". 尝试搜索“ java自动装箱”。

Note : You can actually use Integer.compare(this.i, oi) which is better, for that case. 注意 :在这种情况下,您可以实际使用更好的Integer.compare(this.i, oi)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM