Java:将数组从最小到最大排序

Java:将数组从最小到最大排序,java,arrays,Java,Arrays,这是我在网站上的第一个问题。我在一个CS班,我的一部分作业是将数组的分数列从最小到最大排序,并以新的从最小到最大的形式打印出数组。无论出于什么原因,我似乎无法从数组中获得分数,我很困惑为什么它不起作用。如果你们中有人能帮助我,那就太棒了。谢谢这是我正在使用的阵列。这两列是id(左侧)和分数(右侧): 这是我到目前为止的代码: import java.io.*; import java.util.*; public class RankingTestScoresDriver { public st

这是我在网站上的第一个问题。我在一个CS班,我的一部分作业是将数组的分数列从最小到最大排序,并以新的从最小到最大的形式打印出数组。无论出于什么原因,我似乎无法从数组中获得分数,我很困惑为什么它不起作用。如果你们中有人能帮助我,那就太棒了。谢谢这是我正在使用的阵列。这两列是id(左侧)和分数(右侧):

这是我到目前为止的代码:

import java.io.*;
import java.util.*;
public class RankingTestScoresDriver
{
public static void main(String args[])
{
    System.out.println("ID     Score");
    File file = new File("prog408a.dat");
    int[] id = new int[24];
    int[] score = new int[24];
    try
    {
        Scanner kbReader = new Scanner(file);
        while (kbReader.hasNextInt())
        {
            for (int i = 0; i < 25; i++)
            {
                id[i] = kbReader.nextInt();
                score[i] = kbReader.nextInt();
            }
        }
    }
    catch (Exception e)
    {

    }

//From here down is the part which is giving me trouble.

    int max;
    max = score[0];
    int index;
    int maxScoreIndex = 0;
    for (int k = 0; k < 25; k++)
    {
    for (index = 0; index < score.length; index++)
    {
        if (score[index] > max)
        {
            max = score[index];
            maxScoreIndex = index;
        }
    }
    System.out.println(maxScoreIndex + "     " + max);
    score[maxScoreIndex] = ((-1)*(max));
    }
}
import java.io.*;
导入java.util.*;
公共类RankingTestScoresDriver
{
公共静态void main(字符串参数[])
{
系统输出打印项次(“ID分数”);
File File=新文件(“prog408a.dat”);
int[]id=新的int[24];
整数[]分数=新整数[24];
尝试
{
扫描仪kbReader=新扫描仪(文件);
while(kbReader.hasNextInt())
{
对于(int i=0;i<25;i++)
{
id[i]=kbReader.nextInt();
分数[i]=kbReader.nextInt();
}
}
}
捕获(例外e)
{
}
//从这里开始就是给我带来麻烦的部分。
int max;
最大值=分数[0];
整数指数;
int maxScoreIndex=0;
对于(int k=0;k<25;k++)
{
对于(索引=0;索引最大值)
{
max=得分[指数];
maxScoreIndex=索引;
}
}
System.out.println(maxScoreIndex+“”+max);
分数[maxScoreIndex]=(-1)*(max));
}
}
}

目前,它正在将此作为输出:

ID分数 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12 599 12599

最终的输出应该是数组的分数,但至少是最大的顺序


如果您能帮助我修复我正在苦苦挣扎的代码,我们将不胜感激。

您可以编写一个实现
Comparable
的类来简化它

class Score implements Comparable<Score> {
    private int id, score;
    Score(int id, int score) {
        this.id = id;
        this.score = score;
    }

    @override
    public int compareTo(Score o) {
        return score - o.score;
    }
}

将在您尝试排序的部分起作用,我建议您对数组调用array.sort(),如下所示:

Arrays.sort(score);
数组排序后,使用某种循环从一开始打印每个元素:

for(int i = 0; i < 25; i++)
{
    System.out.println(i + "     " + score[i]);
}
for(int i=0;i<25;i++)
{
System.out.println(i+“”+分数[i]);
}

这应该对你有好处。如果你不需要一个新奇的排序算法,只需要使用一个已经为你编码好的算法:)

你需要对数组进行排序。现在你的代码没有做到这一点,它只是跟踪最大的价值

我想你不必担心这里的执行时间。换句话说,如果算法需要很长时间来执行一个大的输入,这对你的教授来说并不重要

有许多方法可以对数组进行排序。有一种方法叫做。这不是很有效,但会对你起作用,这可能是你的教授现在想要的

这个想法非常简单:你发现你在整个数组中持有的最低值,然后用你的当前位置交换这个值。假设您拥有以下阵列:

[2,4,3,5,1]

您当前的索引是
0
。您将遍历整个数组,以找到所有数组中的最小值。您将发现保存值
1
的索引4包含最低值。因此,交换当前索引(索引
0
,值
2
)。现在,阵列如下所示:

Arrays.sort(score);
[1,4,3,5,2]

现在转到下一个索引,即
1
。你不需要再看索引
0
,因为你已经发现他是最低的值,现在你想要第二个最低的值。您可以执行与以前相同的操作:从索引1到索引4找到最小值,然后交换它们。你会一直这样做直到你到达最后一个索引,在那里,你现在可以猜到,它总是最大的值


首先确保第一个值是正确的。一旦得到它,就转到循环,循环遍历其他值。

工作代码:在java中对列值的二维数组进行排序。

在这里,我们在java.util.Arrays类中使用了重载排序方法,它接受两个参数:排序的数组java.util.Comparator对象

import java.util.Arrays;
import java.util.Comparator;

public class SortArray {

    Integer array[][] = { { 365, 265 }, { 222, 223 }, { 306, 262 },
            { 003, 559 }, { 004, 560 }, { 203, 224 }, { 113, 243 },
            { 208, 242 }, { 213, 229 }, { 115, 257 }, { 302, 242 },
            { 223, 230 }, { 001, 599 }, { 311, 256 }, { 323, 245 },
            { 321, 245 }, { 123, 253 }, { 104, 239 }, { 002, 513 },
            { 112, 239 }, { 207, 228 }, { 325, 246 }, { 116, 246 },
            { 218, 243 }, { 110, 238 } };

    SortArray() {
        System.out.println("Before sorting");
        // show the contents of array
        displayArray();

        // sort array on score(second column)
        Arrays.sort(array, new Comparator<Integer[]>() {
            @Override
            public int compare(Integer[] o1, Integer[] o2) {
                Integer v1 = o1[1];
                Integer v2 = o2[1];
                // reverse sort on quantity
                return v1.compareTo(v2);
            }
        });
        // display array after sort
        System.out.println("After sorting on score in ascending order");
        displayArray();

    }

    public void displayArray() {

        System.out.println("-------------------------------------");
        System.out.println(" Index \t\t score");
        for (int i = 0; i < array.length; i++) {
            Integer[] sorted = array[i];
            System.out.println(sorted[0] + "\t\t" + sorted[1]);
        }
        System.out.println("-------------------------------------");
    }

    public static void main(String[] args) {
        new SortArray();
    }

}
导入java.util.array;
导入java.util.Comparator;
公营索塔雷酒店{
整数数组[][]={365,265},{222,223},{306,262},
{ 003, 559 }, { 004, 560 }, { 203, 224 }, { 113, 243 },
{ 208, 242 }, { 213, 229 }, { 115, 257 }, { 302, 242 },
{ 223, 230 }, { 001, 599 }, { 311, 256 }, { 323, 245 },
{ 321, 245 }, { 123, 253 }, { 104, 239 }, { 002, 513 },
{ 112, 239 }, { 207, 228 }, { 325, 246 }, { 116, 246 },
{ 218, 243 }, { 110, 238 } };
Sortaray(){
System.out.println(“排序前”);
//显示数组的内容
displayArray();
//按分数排序数组(第二列)
sort(数组,新的比较器(){
@凌驾
公共整数比较(整数[]o1,整数[]o2){
整数v1=o1[1];
整数v2=o2[1];
//按数量反向排序
返回v1.compareTo(v2);
}
});
//排序后显示数组
System.out.println(“按升序对分数排序后”);
displayArray();
}
公共void displayArray(){
System.out.println(“-----------------------------------------”;
System.out.println(“索引\t\t分数
import java.util.Arrays;
import java.util.Comparator;

public class SortArray {

    Integer array[][] = { { 365, 265 }, { 222, 223 }, { 306, 262 },
            { 003, 559 }, { 004, 560 }, { 203, 224 }, { 113, 243 },
            { 208, 242 }, { 213, 229 }, { 115, 257 }, { 302, 242 },
            { 223, 230 }, { 001, 599 }, { 311, 256 }, { 323, 245 },
            { 321, 245 }, { 123, 253 }, { 104, 239 }, { 002, 513 },
            { 112, 239 }, { 207, 228 }, { 325, 246 }, { 116, 246 },
            { 218, 243 }, { 110, 238 } };

    SortArray() {
        System.out.println("Before sorting");
        // show the contents of array
        displayArray();

        // sort array on score(second column)
        Arrays.sort(array, new Comparator<Integer[]>() {
            @Override
            public int compare(Integer[] o1, Integer[] o2) {
                Integer v1 = o1[1];
                Integer v2 = o2[1];
                // reverse sort on quantity
                return v1.compareTo(v2);
            }
        });
        // display array after sort
        System.out.println("After sorting on score in ascending order");
        displayArray();

    }

    public void displayArray() {

        System.out.println("-------------------------------------");
        System.out.println(" Index \t\t score");
        for (int i = 0; i < array.length; i++) {
            Integer[] sorted = array[i];
            System.out.println(sorted[0] + "\t\t" + sorted[1]);
        }
        System.out.println("-------------------------------------");
    }

    public static void main(String[] args) {
        new SortArray();
    }

}
public static <T> void sort(T[] a,
            Comparator<? super T> c)