简体   繁体   English

如何在数组中找到最高的总值索引?

[英]How to find the highest Total value index in the Array?

here fundememtal,database are two subjects.这里fundememtal,数据库是两个主题。 same element's on both arrays are for the same student. arrays 上的相同元素适用于同一个学生。 ( fundemental[0],database[0]--> this is for the student 1 ). (fundemental[0],database[0]-->这是给学生 1 的)。 i need to find highest(high rank) to lowest rank students from this program.i declare a sort method and pass the total array and create that array total as ascending.just check it out my attachment photo.我需要从该程序中找到最高(高等级)到最低等级的学生。我声明一个排序方法并传递总数组并将该数组总数创建为升序。只需查看我的附件照片即可。 i need to find this rank我需要找到这个排名

here is my code.if anybody have unclear, please ask me.这是我的代码。如果有人不清楚,请问我。

import java.util.*;
class Remove{
    public static void main(String args[]){
        int [] fundemental={54,34,35,65,87,37};
        int database[]={67,56,45,57,78,89};
        int[] total=new int[database.length];

        for (int i = 0; i < database.length; i++){
            total[i]=fundemental[i]+database[i];
        }
            int [] arrayTot=sort(total);

            int index=0;

            for (int i = 0; i < total.length; i++){
                for (int j = 0; j < total.length; j++){
                    if(total[i]==(fundemental[j]+database[j])) 
                    index=i;
                }

            }
    }

    public static int[] sort(int[]total){
        for (int i = total.length; i >0; i--){
            int min=total[0];
            int index=0;
            for (int j =1; j < i; j++)
            {
                if(total[j]<min){
                min=total[j];
                index=j;    

                }
            }
            total[index]=total[i-1];
            total[i-1]=min;
        }
        return total;

    }
}

if anybody has an another idea for find highest rank to lower rand from student's two subject, please code me.如果有人有另一个想法从学生的两个科目中找到最高排名以降低兰特,请给我编码。

You can use insertion sort to the small array size and swap all arrays您可以对小数组大小使用插入排序并交换所有 arrays

 public static void main(String[] args) {
        int[] fundamentals = { 54, 34, 35, 65, 87, 37 };
        int database[] = { 67, 56, 45, 57, 78, 89 };
        int len = database.length;
        int[] total = new int[len];

        for (int i = 0; i < len; i++) {
            total[i] = fundamentals[i] + database[i];
        }

        for (int i = 1; i < len; i++) {
            for (int j = i; j > 0 && total[j] > total[j - 1]; j--) {
                swap(total, fundamentals, database, j, j - 1);
            }
        }
    }

    static void swap(int[] total, int[] fundamentals, int[] database, int i, int j) {
        int temp = total[j];
        total[j] = total[i];
        total[i] = temp;

        temp = fundamentals[j];
        fundamentals[j] = fundamentals[i];
        fundamentals[i] = temp;

        temp = database[i - 1];
        database[i - 1] = database[i];
        database[i] = temp;
    }

, output , output

[165, 126, 122, 121, 90, 80]
[87, 37, 65, 54, 34, 35]
[78, 89, 57, 67, 56, 45]

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

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