java 找不到适合 sort(int[],<anonymous Comparator<Integer>>) 的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29583035/
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
no suitable method found for sort(int[],<anonymous Comparator<Integer>>)
提问by Junior
Algorithm question: Given a list of non negative integers, arrange them such that they form the largest number.
算法题:给定一个非负整数列表,将它们排列成最大的数。
For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.
例如,给定 [3, 30, 34, 5, 9],最大的形成数是 9534330。
Note: The result may be very large, so you need to return a string instead of an integer.
注意:结果可能非常大,所以需要返回一个字符串而不是一个整数。
public class Solution {
public String largestNumber(int[] num) {
Arrays.sort(num, new java.util.Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
String s1 = String.valueOf(a), s2 = String.valueOf(b);
return Integer.parseInt(s1 + s2) - Integer.parseInt(s2 + s1);
}
});
StringBuilder builder = new StringBuilder();
for (int i = num.length - 1; i >= 0; i--) builder.append(num[i]);
return builder.toString();
}
}
Result: Line 3: error: no suitable method found for sort(int[],<anonymous Comparator<Integer>>)
结果:第 3 行:错误:找不到适合 sort(int[],<anonymous Comparator<Integer>>) 的方法
Does anyone know how to modify it? Thanks!
有谁知道怎么修改?谢谢!
Thank you for all your detail answers, I have modified my code as
感谢您提供所有详细答案,我已将代码修改为
public String largestNumber(int[] num) {
int N = num.length;
String[] aux = new String[N];
for (int i = 0; i < N; i++) aux[i] = String.valueOf(num[i]); // int[] -> String[]
Arrays.sort(aux, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return (int) (Long.parseLong(a + b) - Long.parseLong(b + a)); // note the overflow of int, max + max
}
});
StringBuilder builder = new StringBuilder();
for (int i = N - 1; i >= 0; i--) builder.append(aux[i]);
int sub = 0;
for ( ; sub < builder.length() - 1 && builder.charAt(sub) == '0'; sub++);
return builder.substring(sub).toString();
}
And I am still trying to find a way to avoid using extra space.
我仍在努力寻找避免使用额外空间的方法。
回答by dasblinkenlight
The reason for this is that int
and Integer
are different types. int
is a primitive, and Integer
is its object wrapper. Comparator<T>
works only with objects; it does not work with primitives.
这样做的原因是int
和Integer
是不同的类型。 int
是一个原始类型,并且Integer
是它的对象包装器。Comparator<T>
仅适用于对象;它不适用于原语。
Put int
s in a container of Integer
s, say, an ArrayList<Integer>
, and use your comparator to solve the problem.
将int
s放入s 的容器中Integer
,例如 an ArrayList<Integer>
,然后使用比较器来解决问题。
Note that your approach may fail for very large integers, because concatenating two valid int
s may produce a number too large for an int
to hold. You could return (s1+s2).compareTo(s2+s1)
instead for lexicographical comparison, which is identical to numeric comparison for numbers of the same length.
请注意,对于非常大的整数,您的方法可能会失败,因为连接两个有效的int
s 可能会产生一个太大而int
无法容纳的数字。您可以返回(s1+s2).compareTo(s2+s1)
进行字典序比较,这与相同长度的数字的数字比较相同。
回答by Alexander R.
You must use Integer
您必须使用整数
Integer[] nums = new Integer[]{3, 30, 34, 5, 9};
public class Solution {
public String largestNumber(Integer[] num) {
Arrays.sort(num, new java.util.Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
String s1 = String.valueOf(a), s2 = String.valueOf(b);
return Integer.parseInt(s1 + s2) - Integer.parseInt(s2 + s1);
}
});
StringBuilder builder = new StringBuilder();
for (int i = num.length - 1; i >= 0; i--) builder.append(num[i]);
return builder.toString();
}
}