如何使用参数 Math.abs(b)-Math(a) 进行排序。

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

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[],&lt;anonymous Comparator&lt;Integer&gt;&gt;)
                 Arrays.sort(ar, new Comparator&lt;Integer&gt;(){
                       ^
    method Arrays.&lt;T#1&gt;sort(T#1[],Comparator&lt;? super T#1&gt;) is not applicable
      (inference variable T#1 has incompatible bounds
        equality constraints: int
        lower bounds: Integer,Object)
    method Arrays.&lt;T#2&gt;sort(T#2[],int,int,Comparator&lt;? super T#2&gt;) 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--&gt;0){

                int n = sc.nextInt(); 
                int ar[] = new int[n];

                for(int  i = 0;i&lt;n;i++)
                    ar[i] = sc.nextInt();;
                
 
                 Arrays.sort(ar, new Comparator&lt;Integer&gt;(){

                    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-- &gt; 0) {
			ar[t] = sc.nextInt();
		}

		Arrays.sort(ar, new Comparator&lt;Integer&gt;() {

			public int compare(Integer a, Integer b) {
				return Math.abs(b) - Math.abs(a);
			}
		});

		for (int i = 0; i &lt; 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.

huangapple
  • 本文由 发表于 2020年9月13日 13:38:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63867531.html
匿名

发表评论

匿名网友

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

确定