繁体   English   中英

对数组进行排序(不排序或创建对象)并保留原始索引

[英]Rank an array (without sorting or creating an object) and keep the original index

我有一个程序应该从文件中读取学生 ID 和 GPA,将它们放在 2 个单独的 arrays 中,按 GPA 范围对学生进行分类,制作该分类的直方图,最后根据 GPA 对它们进行排名(考虑关系),但仍按它们在文件中的顺序打印它们。

我想我已经弄清楚了程序的每一部分,但我有一个问题。 我不知道如何仅获取相关学生 ID 和 GPA 的排名以与他们一起打印。 相反,我只能在一行上一遍又一遍地打印包含每个学生/ GPA 排名的一行。

示例 output:

S8887184

3.2

[228、835、655、774、579、602、873、884、966、592、708、865...等]

所需的 output:

S8887184

3.2

228

在上面的输出中,第一行是学生 ID,第二行是 GPA,第三行是其他学生的排名。 如果我还可以结合一种方式来表明排名是平局,以及与多少其他学生排名并列,那也将是理想的。

下面是我的代码供参考。 如果您能帮我解决这个问题,将不胜感激! 谢谢!

public static void main(String[] args) throws Exception
{
    Scanner gpadata;
    String snum;
    double gpa;
    String[] IDs = new String[1000];
    double[] GPAs = new double[1000];
    int counter;
    counter = 0;
    int[] gpaGroup = new int[8];

    gpadata = new Scanner(new File("studentdata.txt"));

    while (gpadata.hasNext())
    {
        snum = gpadata.next();
        gpa = gpadata.nextDouble();

        IDs[counter] = snum;
        GPAs[counter] = gpa;

        //Group students by GPA range
        if (GPAs[counter] >= 0.0 && GPAs[counter] < 0.5)
            gpaGroup[0]++;
        else if (GPAs[counter] >= 0.5 && GPAs[counter] < 1.0)
            gpaGroup[1]++;
        else if (GPAs[counter] >= 1.0 && GPAs[counter] < 1.5)
            gpaGroup[2]++;
        else if (GPAs[counter] >= 1.5 && GPAs[counter] < 2.0)
            gpaGroup[3]++;
        else if (GPAs[counter] >= 2.0 && GPAs[counter] < 2.5)
            gpaGroup[4]++;
        else if (GPAs[counter] >= 2.5 && GPAs[counter] < 3.0)
            gpaGroup[5]++;
        else if (GPAs[counter] >= 3.0 && GPAs[counter] < 3.5)
            gpaGroup[6]++;
        else
            gpaGroup[7]++;

        counter++;

    }

    //Round number of students in each GPA group to nearest 10
    int histogram = Math.round(gpaGroup[0]/10);
    int histogram1 = Math.round(gpaGroup[1]/10);
    int histogram2 = Math.round(gpaGroup[2]/10);
    int histogram3 = Math.round(gpaGroup[3]/10);
    int histogram4 = Math.round(gpaGroup[4]/10);
    int histogram5 = Math.round(gpaGroup[5]/10);
    int histogram6 = Math.round(gpaGroup[6]/10);
    int histogram7 = Math.round(gpaGroup[7]/10);

    //Print out GPA group, number of students in that group, and histogram
    System.out.println("GPA Range       #    Histogram");
    System.out.println("0.00 to 0.49   " + gpaGroup[0] + "    " +
            toStars(histogram));
    System.out.println("0.50 to 0.99   " + gpaGroup[1] + "    " +
            toStars(histogram1));
    System.out.println("1.00 to 1.49   " + gpaGroup[2] + "   " +
            toStars(histogram2));
    System.out.println("1.50 to 1.99   " + gpaGroup[3] + "   " +
            toStars(histogram3));
    System.out.println("2.00 to 2.49   " + gpaGroup[4] + "   " +
            toStars(histogram4));
    System.out.println("2.50 to 2.99   " + gpaGroup[5] + "   " +
            toStars(histogram5));
    System.out.println("3.00 to 3.49   " + gpaGroup[6] + "   " +
            toStars(histogram6));
    System.out.println("3.50 to 4.00   " + gpaGroup[7] + "   " +
            toStars(histogram7));

    //Add blank lines between histogram and part 2
    System.out.println();
    System.out.println();

    //print rank
    System.out.println("Student ID Number, GPA, and Class Rank");
    System.out.println();
    for (int k=0; k < IDs.length; k++){
    System.out.println(IDs[k]);
    System.out.println(GPAs[k]);
    System.out.println(Arrays.toString(getRanksArray(GPAs)));
    System.out.println();
    k++;

}
}

//Method to convert rounded # of students to histogram
public static String toStars(int number)
{
    StringBuilder temp = new StringBuilder();
    for(int i=0; i<number; i++){
        temp.append("*");
    }
    return temp.toString();
}


//Method to determine students class rank
public static int[] getRanksArray(double[] array) 
{
int[] result = new int[array.length];

for (int i = 0; i < array.length; i++) {
    int count = 0;
    for (int j = 0; j < array.length; j++) {
        if (array[j] > array[i]) {
            count++;
        }
    }
    result[i] = count + 1;
}
return result;
}   

我建议将 GPA 计数包装在 object 中,以便您可以将索引与计数相关联。

class GpaCount {
  private int count = 0;
  private final int index;

  public GpaCount(int index) {
    this.index = index;
  }

  public int getCount() {
    return count;
  }

  public void increment() {
    count++;
  }

  public int getIndex() {
    return index;
  }
}

然后,您可以使用自定义Comparator使用Collections.sort()对计数进行排序:

List<GpaCount> gpaCounts = new ArrayList<>();

// populate gpaCount (insert your GPA counting logic here)

Comparator<GpaCount> comparator = (GpaCount o1, GpaCount o2) -> Integer.compare(o1.getCount(), o2.getCount());
Collections.sort(gpaCounts, comparator);

// now gpaCounts is sorted by count

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM