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

Java 对数组进行排序(不排序或创建对象)并保留原始索引,java,arrays,sorting,ranking,Java,Arrays,Sorting,Ranking,我有一个程序,应该从一个文件中读取学生ID和GPA,将它们放在两个单独的数组中,根据GPA范围对学生进行分类,制作这种分类的直方图,最后根据GPA对它们进行排序(考虑联系),但仍然按文件中的顺序打印它们 我想我已经弄清楚了计划的每一部分,但我有一个问题。我不知道如何只获得相关学生ID和GPA的排名,以便打印出来。相反,我只能在一行上反复打印一行,其中包含每个学生/GPA的排名 示例输出: S887184 3.2 [2288356557745796028738849665270865…等等] 期望

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

我想我已经弄清楚了计划的每一部分,但我有一个问题。我不知道如何只获得相关学生ID和GPA的排名,以便打印出来。相反,我只能在一行上反复打印一行,其中包含每个学生/GPA的排名

示例输出:

S887184

3.2

[2288356557745796028738849665270865…等等]

期望输出:

S887184

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;
}   
publicstaticvoidmain(字符串[]args)引发异常
{
扫描仪gpadata;
串紧;
双gpa;
字符串[]ID=新字符串[1000];
double[]GPAs=新的double[1000];
整数计数器;
计数器=0;
int[]gpaGroup=newint[8];
gpadata=新扫描仪(新文件(“studentdata.txt”);
while(gpadata.hasNext())
{
snum=gpadata.next();
gpa=gpadata.nextDouble();
IDs[计数器]=snum;
GPAs[计数器]=gpa;
//按GPA范围对学生分组
如果(GPAs[计数器]>=0.0&&GPAs[计数器]<0.5)
gpaGroup[0]++;
否则如果(GPAs[计数器]>=0.5&&GPAs[计数器]<1.0)
gpaGroup[1]++;
否则如果(GPAs[计数器]>=1.0&&GPAs[计数器]<1.5)
gpaGroup[2]++;
否则如果(GPAs[计数器]>=1.5&&GPAs[计数器]<2.0)
gpaGroup[3]++;
否则如果(GPAs[计数器]>=2.0&&GPAs[计数器]<2.5)
gpaGroup[4]++;
否则如果(GPAs[计数器]>=2.5&&GPAs[计数器]<3.0)
gpaGroup[5]++;
否则如果(GPAs[计数器]>=3.0&&GPAs[计数器]<3.5)
gpaGroup[6]++;
其他的
gpaGroup[7]++;
计数器++;
}
//将每个GPA组的学生人数四舍五入至最接近的10人
int直方图=数学四舍五入(gpaGroup[0]/10);
int historogram1=数学四舍五入(gpaGroup[1]/10);
int historogram2=数学四舍五入(gpaGroup[2]/10);
int historogram3=数学四舍五入(gpaGroup[3]/10);
int historogram4=数学四舍五入(gpaGroup[4]/10);
int historogram5=数学四舍五入(gpaGroup[5]/10);
int historogram6=数学四舍五入(gpaGroup[6]/10);
int historogram7=数学四舍五入(gpaGroup[7]/10);
//打印出GPA组、该组学生人数和直方图
System.out.println(“GPA范围#直方图”);
System.out.println(“0.00到0.49”+gpaGroup[0]+”“+
托斯塔尔(直方图);
System.out.println(“0.50到0.99”+gpaGroup[1]+”“+
toStars(histogram1));
System.out.println(“1.00至1.49”+gpaGroup[2]+”“+
toStars(histogram2));
System.out.println(“1.50到1.99”+gpaGroup[3]+”“+
toStars(Historogram3));
System.out.println(“2.00至2.49”+gpaGroup[4]+”“+
toStars(histogram4));
System.out.println(“2.50到2.99”+gpaGroup[5]+”“+
toStars(histogram5));
System.out.println(“3.00至3.49”+gpaGroup[6]+”“+
toStars(historogram6));
System.out.println(“3.50到4.00”+gpaGroup[7]+”“+
toStars(Historogram7));
//在直方图和第2部分之间添加空行
System.out.println();
System.out.println();
//印刷品等级
System.out.println(“学生ID号、GPA和班级等级”);
System.out.println();
对于(int k=0;k
我建议将GPA计数包装在一个对象中,以便您可以将索引与计数关联起来

class-GpaCount{
私有整数计数=0;
私有最终整数指数;
公共GPA帐户(整数索引){
这个指数=指数;
}
public int getCount(){
返回计数;
}
公共空间增量(){
计数++;
}
public int getIndex(){
收益指数;
}
}
然后,您可以使用自定义的比较器对计数进行排序,方法是使用
Collections.sort()

List gpaCounts=new ArrayList();
//填充GPA计数(在此处插入GPA计数逻辑)
Comparator Comparator=(GpaCount o1,GpaCount o2)->Integer.compare(o1.getCount(),o2.getCount());
Collections.sort(gpaCounts、comparator);
//现在GPA计数是按计数排序的

如果您想对现有代码进行一些评论,那么这将是一个很好的起点