java 不兼容的类型:int[] 不能转换为 Comparable<Object>[]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26517632/
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-11-02 10:03:51  来源:igfitidea点击:

Incompatible types: int[] cannot be converted to Comparable<Object>[]

javagenericscomparable

提问by Peterxwl

I am new to generics and casting issues. I try to sort any type array that is comparable. The error is as the title; and the code is as below: and error is same for integer. What is the problem and why?

我是泛型和铸造问题的新手。我尝试对任何可比较的类型数组进行排序。错误如标题;代码如下:整数错误相同。有什么问题,为什么?

class Sort{
  public static void selectionSort(Comparable<Object>[] input)
  {/* selection Sort Alg*/}
  public static void main((String[] args){
    int[] intArray = {2,3,5,1};
    Sort.selectionSort(intArray);
  }
}

回答by Stefano Sanfilippo

You have two issues here:

你在这里有两个问题:

  1. intis a POD, not an object. Boxing and unboxing from int[]to corresponding Integer[]is not automatically performed. You need to declare intArrayas:

    Integer[] intArray = {2,3,5,1};
    
  2. Integerimplements Comparable<Integer>, not Comparable<Object>. The two specializations are different and incompatible types. The right way to declare selectionSortis by using generics, as you suggest in the title:

    public static <T extends Comparable<T>> void selectionSort(T[] input)
    
  1. int是一个 POD,而不是一个对象。不会自动执行从int[]到对应的装箱和拆箱Integer[]。您需要声明intArray为:

    Integer[] intArray = {2,3,5,1};
    
  2. Integer实现Comparable<Integer>,不是Comparable<Object>。这两种专业化是不同且不兼容的类型。正确的声明方法selectionSort是使用泛型,正如您在标题中所建议的:

    public static <T extends Comparable<T>> void selectionSort(T[] input)
    

回答by Damian Leszczyński - Vash

You have two issues in your code snippet

您的代码片段中有两个问题

The fist one is with the argument of your method, lets look on this example

第一个是你的方法的参数,让我们看看这个例子

void simple(Comparable<Object> input) {}

void simple(Comparable<Object> input) {}

This simple method expect a instance of Comparable<Object>.

这个简单的方法需要一个Comparable<Object>.

To create a instance for that you could implement class like:

要为此创建一个实例,您可以实现如下类:

class MyComparable implements Comparable<Object> {}

class MyComparable implements Comparable<Object> {}

What you must by aware is that genericType ( className ), assure your type safety. This mean that:

您必须知道的是 genericType ( className ) 可以确保您的类型安全。这意味着:

  • Comparable<String>can compare only String objects
  • Comparable<Object>can compare only Object objects
  • Comparable<String>只能比较 String 对象
  • Comparable<Object>只能比较 Object 对象

You can not really on class hierarchy in term of generic parameters.

就泛型参数而言,您无法真正了解类层次结构。

The class Integerimplements Comparable<Integer>so to be able to use that you can do this:

该类Integer实现Comparable<Integer>以便能够使用您可以执行以下操作:

void simple(Comparable<Integer> input) {}

void simple(Comparable<Integer> input) {}

The you will be able pass everything that implements Comparable<Integer>.

您将能够通过实现Comparable<Integer>.

With arrays is the same rule,

与数组是相同的规则,

void array(Comparable<Integer>[] input) {}

void array(Comparable<Integer>[] input) {}

but what you should keep in mind is that int[]is not the same as Integer[]JVM use different operations for those types.

但是您应该记住的是,int[]这与Integer[]JVM 对这些类型使用不同的操作不同。

An int[]stores primitive integer values. And Integer[]stores references to Integerclass.

Anint[]存储原始整数值。并Integer[]存储对Integer类的引用。

  • Comparable<Integer>[] array = {1,2,3,4};is not allowed
  • Comparable<Integer>[] array = new Integer[] {1,2,3,4};is valid statement
  • Comparable<Integer>[] array = {1,2,3,4};不被允许
  • Comparable<Integer>[] array = new Integer[] {1,2,3,4};是有效的陈述

回答by rickyalbert

Generics only work with Object, not with primitive types. Fortunately, you can find some useful wrapper classes into the package java.lang, like the object Integer. Your code will become:

泛型仅适用于 Object,不适用于原始类型。幸运的是,您可以在包 java.lang 中找到一些有用的包装类,例如对象 Integer。您的代码将变为:

Integer[] intArray = new Integer[]{2,3,5,1};
Sort.selectionSort(intArray);

I suggest to change the parameter into the selectionSort: you don't want a Comparable<Object>, but a generic T<extends Comparable<? super T>>; in other words you need a parameter which extends a class which can compare T and theirs superclasses

我建议将参数更改为 selectionSort:您不想要一个Comparable<Object>,而是一个通用的T<extends Comparable<? super T>>; 换句话说,您需要一个扩展类的参数,该类可以比较 T 及其超类

回答by Riadh

int is a primitive, you should use Integer. You have an already sort method that sorts Integer:

int 是一个原始类型,您应该使用 Integer。你已经有一个排序整数的排序方法:

class Sort{
  public static void main(String[] args){
        List<Integer> intList = new ArrayList<Integer>();
        intList.add(2);
        intList.add(3);
        intList.add(5);
        intList.add(1);
        Collections.sort(intList);
        System.out.println(intList.toString());
      }
}