【问题标题】:public static <T> void sort(T[] a, Comparator<? super T> c) is not working [duplicate]公共静态 <T> void sort(T[] a, Comparator<? super T> c) 不起作用[重复]
【发布时间】:2015-01-31 05:23:59
【问题描述】:

我使用比较器的 Arrays.sort() 命令不起作用。我想按降序对数组进行排序。我的程序如下:

import java.util.*;
import java.io.*;

class MinScalProd
{
public static void main(String[] ar) throws IOException
{
Scanner input=new Scanner(new File("Min.in"));
FileWriter fw = new FileWriter("Min.out");
int T=input.nextInt();
    for(int cases = 1; cases <= T; cases++)
    {
        int n=input.nextInt();
        int[] v1=new int[n];
        int[] v2=new int[n];
        int sp=0;
        for(int i=0;i<n;i++)


        {
            v1[i]=input.nextInt();
        }
        for(int i=0;i<n;i++)
        {
            v2[i]=input.nextInt();
        }
        Comparator comp = Collections.reverseOrder();
        Arrays.sort(v1);
        Arrays.sort(v2,comp);/*here it is giving error(Cannot find symbol)*/
        for(int i=0;i<n;i++)
        {
        sp=sp+v1[i]*v2[i];
        }
        fw.write("Case #" + cases + ": "+sp+"\n");


    }
fw.flush();
fw.close();
}
}

希望大家尽快解决

【问题讨论】:

    标签: java sorting


    【解决方案1】:

    sort(T[] a, Comparator&lt;? super T&gt; c) 只接受引用类型的数组,它不适用于原始数组。您可以将v2 更改为Integer 的数组。

    改变

    int[] v2=new int[n];
    

    Integer[] v2=new Integer[n];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多