java.lang.ClassCastException: 不能转换为 java.lang.Integer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22548325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:24:30  来源:igfitidea点击:

java.lang.ClassCastException: cannot be cast to java.lang.Integer

java

提问by user3444455

I wanted to create a compareTomethod but it gives me an error. Can anyone help me with this ?

我想创建一个compareTo方法,但它给了我一个错误。谁能帮我这个 ?

     package defaultpackage;
     public class HuffmanEntry implements Entry<Integer, String> {
     private String c;  
     private Integer freq;

     public HuffmanEntry(String c, Integer freq){    
     this.c = c;
     this.freq = freq;
     }

     @Override
     public int compareTo(Integer arg0) {
    if(getKey()<arg0){
        return -1;
    }else if(getKey()>arg0){
        return 1;
    }else{
        return 0;   
    }
}

      @Override
      public Integer getKey() {
      return freq;
      }

Here's my error:

这是我的错误:

Exception in thread "main" java.lang.ClassCastException: defaultpackage.HuffmanEntry cannot be
cast to java.lang.Integer
at defaultpackage.HuffmanEntry.compareTo(HuffmanEntry.java:1)

采纳答案by Stephen C

There are serious problems with the quality of the "evidence" you have provided us:

您提供给我们的“证据”质量存在严重问题:

  1. The stacktrace seems to say that the exception is thrown at line 1 of HuffmanEntry.java, and that is impossible.

  2. The stacktrace seems to say that your code is attempting to cast a HuffmanEntryobject to an Integerin compareTo, but (according to the source code) that cannot happen in that method. All objects used in that method have a declared type of Integer, and there are no explicity typecasts.

  3. The stacktrace shows that compareTois the one and only method call, and that is impossible.

  1. 堆栈跟踪似乎说在第 1 行抛出异常HuffmanEntry.java,这是不可能的。

  2. 堆栈跟踪似乎说您的代码正试图将HuffmanEntry对象强制转换为Integerin compareTo,但是(根据源代码)在该方法中不会发生。该方法中使用的所有对象的声明类型为Integer,并且没有明确的类型转换。

  3. 堆栈跟踪显示这compareTo是唯一的方法调用,这是不可能的。

(3 impossible things before lunch? Nah! Don't buy it!)

(午饭前三件不可能的事?不!别买!)

In short, the evidence you have shown us is not real. It has been tweaked / mangled, and in ways that make it impossible to interpret.

简而言之,您向我们展示的证据并不真实。它已被调整/损坏,并且以使其无法解释的方式进行。

Please provide a real (complete and unedited) stacktrace, and source code that precisely matches the compiled code you ran to produce the stacktrace. Also any compilation errors.

请提供真实的(完整且未经编辑的)堆栈跟踪,以及与您运行以生成堆栈跟踪的编译代码精确匹配的源代码。还有任何编译错误。

Alternatively, provide us with an SSCCE that we can run ourselves to reproduce this facially impossible behaviour / evidence.

或者,为我们提供一个 SSCCE,我们可以自己运行它来重现这种面部不可能的行为/证据。



Looking at the code you linked to, I think I understand the general problem ... if not the specific one.

查看您链接到的代码,我想我了解一般问题......如果不是具体问题。

Basically, you are mixing generic and non-generic code and (you must be) ignoring the resulting compiler warnings about unchecked conversions, use of raw types and so on. The result is that errors that should be picked up by the Java compile-time type checker are not being reported.

基本上,您正在混合通用和非通用代码,并且(您必须)忽略有关未经检查的转换、使用原始类型等的编译器警告。结果是没有报告应该由 Java 编译时类型检查器检测到的错误。

For example, in MyHeap.java, you write:

例如,在 中MyHeap.java,你写:

private ArrayList<Entry> entryList; 

But Entryis actually a generic interface, meaning that ArrayList is actually being interpreted as ArrayList<Entry<Object, Object>>where Objectis the raw type corresponding to the unbounded type parameters Kand T. Now there is an int compareTo(K)method in the Entryinterface ... and the raw type signature is int compareTo(Object).

Entry实际上是一个泛型接口,这意味着 ArrayList 实际上被解释为无界类型参数和对应的原始类型ArrayList<Entry<Object, Object>>在哪里。现在接口中有一个方法......原始类型签名是.ObjectKTint compareTo(K)Entryint compareTo(Object)

Now look at

现在看看

if (entryList.get(i*2).compareTo(entryList.get(i*2+1))==-1){ 

Note that you are effectively calling Entry.compareTO(Object)passing another Entryas the argument. The compiler has said OK .... because it thinksyou are using raw types at that point. But if you had been using generics properly, the compiler would have realized that you are calling Entry<K, V>.compareTo(Entry<K, V>)where the generic method signature is really Entry<K, V>.compareTo(V). That would have been flagged as a compilation error.

请注意,您实际上是在调用Entry.compareTO(Object)传递另一个Entry作为参数。编译器说 OK .... 因为它认为此时您正在使用原始类型。但是,如果您一直正确使用泛型,编译器就会意识到您正在调用Entry<K, V>.compareTo(Entry<K, V>)泛型方法签名实际上是Entry<K, V>.compareTo(V). 那会被标记为编译错误。

I suspect that this explains the strange exception too. The Java compiler has added some hiddenruntime typecasts to make sure that the JVM's core runtime type safety is not compromised. You are seeing the exception when one of those runtime checks fails. And that happens because you effectively ignored or suppressed the compile time checks that should have you told you of your programming error.

我怀疑这也解释了奇怪的异常。Java 编译器添加了一些隐藏的运行时类型转换,以确保 JVM 的核心运行时类型安全不会受到损害。当这些运行时检查之一失败时,您会看到异常。发生这种情况是因为您有效地忽略或抑制了应该告诉您编程错误的编译时检查。

Anyway, the bottom line is that you should NOT ignore compiler warnings about misuse of generics, unchecked conversions, etc. You do so at your peril ... and in this case, you got bitten.

无论如何,最重要的是你不应该忽略关于泛型滥用、未经检查的转换等的编译器警告。你这样做是有危险的......在这种情况下,你被咬了。

回答by ErstwhileIII

You implement Comparable to allow your class objects to be compared, not to compare different objects.

您实现 Comparable 以允许比较您的类对象,而不是比较不同的对象。

Perhaps you want to provide use something like if (Integer x > huffmanObject.getFreq()) instead? Sounds much simpler.

也许您想提供类似 if (Integer x > huffmanObject.getFreq()) 之类的东西?听起来简单多了。