降序排列的数组

huangapple go评论86阅读模式
英文:

Array in Desending Order

问题

错误:(33, 15) java: 没有找到适合的方法以对int[]排序,java.util.Comparator<java.lang.Object>)
     方法java.util.Arrays.<T>sort(T[],java.util.Comparator<? super T>)不适用
       (类型推断变量T具有不兼容的界限
         相等约束:int
         下界:java.lang.Object)
     方法java.util.Arrays.<T>sort(T[],int,int,java.util.Comparator<? super T>)不适用
       (无法推断类型变量T
         (实际参数列表与形式参数列表的长度不同)

我不明白为什么在`Array.sort(num, Collections.reverseOrder())`中的num是错误的?

Scanner s = new Scanner(System.in);

int a = s.nextInt();
int b = s.nextInt();
int c = s.nextInt();

int[] num = new int[3];

num[0] = a;
num[1] = b;
num[2] = c;


Arrays.sort(num, Collections.reverseOrder());
for (int i = 0; i < num.length; i++) {
    System.out.println(num[i] + " ");
}

是否有其他解决方法?
英文:
Error:(33, 15) java: no suitable method found for sort(int[],java.util.Comparator&lt;java.lang.Object&gt;)
    method java.util.Arrays.&lt;T&gt;sort(T[],java.util.Comparator&lt;? super T&gt;) is not applicable
      (inference variable T has incompatible bounds
        equality constraints: int
        lower bounds: java.lang.Object)
    method java.util.Arrays.&lt;T&gt;sort(T[],int,int,java.util.Comparator&lt;? super T&gt;) is not applicable
      (cannot infer type-variable(s) T
        (actual and formal argument lists differ in length))

I don't understand why num in Array.sort(num, Collections.reverseOrder()) is wrong?

    Scanner s = new Scanner(System.in);

    int a = s.nextInt();
    int b = s.nextInt();
    int c = s.nextInt();

    int[] num = new int[3];

    num[0] = a;
    num[1] = b;
    num[2] = c;


    Arrays.sort(num, Collections.reverseOrder());
    for (int i = 0; i &lt; num.length; i++) {
        System.out.println(num[i] + &quot; &quot;);
    }

is there any other way to solve it?

答案1

得分: 2

Arrays.sort方法没有接受int[]Comparator的版本,接受Comparator的版本需要一个Object数组或其子类的数组(例如Integer[])作为参数。

背景信息:类型变量(泛型)不能引用原始类型,所以<T> void sort(T[], Comparator<? super T>)对于int[]无效。

英文:

There is no Arrays.sort method that accepts an int[] and a Comparator - the one that accepts a Comparator requests an array of Object or of any subclass of Object (example Integer[]).

Background information: a type variable (generic) cannot reference a primitive, so

&lt;T&gt; void sort(T[], Comparator&lt;? super T&gt;)

is not valid for int[].

答案2

得分: 1

我建议您使用Java 8的流(Streams)。我会这样做:

Stream.of(num).sorted(Collections.reverseOrder());

如果您想使用Arrays.sort(),那么只有在第一个参数上有两个额外的选项,即int[],分别是(int fromIndex, int toIndex),这就是您在使用(int[], Comparator)参数时出现异常的原因。

英文:

I can recommend you to use Java 8 Streams. I would do it the following:

Stream.of(num).sorted(Collections.reverseOrder());

If you want to use Arrays.sort() then there is just the optionen for int[] as first parameter to give two more parameters (int fromIndex, int toIndex), which is the reason why you are getting an exception using (int[], Comparator) parameter.

答案3

得分: 0

使用ArrayList替代

import java.util.*;
public class Main {
   public static void main (String[] args) {
     Scanner s = new Scanner(System.in);

    int a = s.nextInt();
    int b = s.nextInt();
    int c = s.nextInt();

      ArrayList<Integer> list = new ArrayList<Integer>();
      list.add(a);
      list.add(b);
      list.add(c);
      Comparator c1 = Collections.reverseOrder();
      Collections.sort(list,c1);
      System.out.println("Desending Order : " + list);
   }
}

这将起作用您可以在初始化时使用数组

在我使用Array时下面的代码有效

import java.util.Arrays; 
import java.util.Collections; 

public class Main 
{ 
    public static void main(String[] args) 
    { 
        Integer[] arr = {2, 1, 3}; 
        Arrays.sort(arr, Collections.reverseOrder()); 
        System.out.printf("Desending Order : %s", Arrays.toString(arr)); 
    } 
}
英文:

Use ArrayList instead

import java.util.*;
public class Main {
   public static void main (String[] args) {
     Scanner s = new Scanner(System.in);

    int a = s.nextInt();
    int b = s.nextInt();
    int c = s.nextInt();

      ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
      list.add(a);
      list.add(b);
      list.add(c);
      Comparator c1 = Collections.reverseOrder();
      Collections.sort(list,c1);
      System.out.println(&quot;Desending Order : &quot; + list);
   }
}

This will do the trick. You can use array in intialization.

The code underneath worked for me using Array

import java.util.Arrays; 
import java.util.Collections; 

public class Main 
{ 
	public static void main(String[] args) 
	{ 
		Integer[] arr = {2, 1, 3}; 
		Arrays.sort(arr, Collections.reverseOrder()); 
		System.out.printf(&quot;Desending Order : %s&quot;, Arrays.toString(arr)); 
	} 
} 

huangapple
  • 本文由 发表于 2020年7月31日 04:48:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63181184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定