英文:
How to sort using parameter Math.abs(b)-Math(a)
问题
我的代码有什么问题?我得到了如下错误:
```none
no suitable method found for sort(int[],<anonymous Comparator<Integer>>)
Arrays.sort(ar, new Comparator<Integer>(){
^
method Arrays.<T#1>sort(T#1[],Comparator<? super T#1>) is not applicable
(inference variable T#1 has incompatible bounds
equality constraints: int
lower bounds: Integer,Object)
method Arrays.<T#2>sort(T#2[],int,int,Comparator<? super T#2>) is not applicable
(cannot infer type-variable(s) T#2
(actual and formal argument lists differ in length))
import java.util.*;
public class B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int ar[] = new int[n];
for(int i = 0;i<n;i++)
ar[i] = sc.nextInt();;
Arrays.sort(ar, new Comparator<Integer>(){
public int compare(int a, int b){
return Math.abs(b)-Math.abs(a);
}
});
}
}
}
<details>
<summary>英文:</summary>
What is wrong with my code? I got errors like:
```none
no suitable method found for sort(int[],<anonymous Comparator<Integer>>)
Arrays.sort(ar, new Comparator<Integer>(){
^
method Arrays.<T#1>sort(T#1[],Comparator<? super T#1>) is not applicable
(inference variable T#1 has incompatible bounds
equality constraints: int
lower bounds: Integer,Object)
method Arrays.<T#2>sort(T#2[],int,int,Comparator<? super T#2>) is not applicable
(cannot infer type-variable(s) T#2
(actual and formal argument lists differ in length))
import java.util.*;
public class B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int ar[] = new int[n];
for(int i = 0;i<n;i++)
ar[i] = sc.nextInt();;
Arrays.sort(ar, new Comparator<Integer>(){
public int compare(int a, int b){
return Math.abs(b)-Math.abs(a);
}
});
}
}
}
答案1
得分: 1
代码存在多个问题。
我已经修复了语法问题和循环问题。虽然我不确定在比较数字的绝对值时您想要实现什么。
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
Integer ar[] = new Integer[t];
while (t-- > 0) {
ar[t] = sc.nextInt();
}
Arrays.sort(ar, new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return Math.abs(b) - Math.abs(a);
}
});
for (int i = 0; i < ar.length; i++)
System.out.println(ar[i]);
}
}
第一次执行:
4
-100
-9
-1000
-6
-1000
-100
-9
-6
第二次执行:
6
1
67
12
5
7
34
67
34
12
7
5
1
顺便说一下,对于这个示例仍然有多个需要注意的事项。比如资源关闭、异常处理等。
英文:
There are multiple issues with the code.
I have fixed the syntax issues and loop issues. Though I am not sure with comparing absolute values of numbers what you want to achieve.
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
Integer ar[] = new Integer[t];
while (t-- > 0) {
ar[t] = sc.nextInt();
}
Arrays.sort(ar, new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return Math.abs(b) - Math.abs(a);
}
});
for (int i = 0; i < ar.length; i++)
System.out.println(ar[i]);
}
}
<!-- language: none -->
First execution:
================
4
-100
-9
-1000
-6
-1000
-100
-9
-6
<!-- language: none -->
Second execution:
================
6
1
67
12
5
7
34
67
34
12
7
5
1
By the way still there still there are multiple things need to take care for this sample. Say resource close, exception handling etc.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论