英文:
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<java.lang.Object>)
method java.util.Arrays.<T>sort(T[],java.util.Comparator<? super T>) is not applicable
(inference variable T has incompatible bounds
equality constraints: int
lower bounds: java.lang.Object)
method java.util.Arrays.<T>sort(T[],int,int,java.util.Comparator<? super T>) 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 < num.length; i++) {
System.out.println(num[i] + " ");
}
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
<T> void sort(T[], Comparator<? super T>)
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<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);
}
}
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("Desending Order : %s", Arrays.toString(arr));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论