检索数组最大值的索引

时间:2015-10-26 13:46:50

标签: java indexing element

假设我有一个元素{1,5,2,3,4}的数组。 我有代码来找到最大值,即5。 我想通过将array [1]替换为array [2],将array [2]替换为array [3]等来从数组中删除此值,然后使用少一个索引创建一个新数组(不重复最后的价值)。 如何找到/陈述数组最大值的索引,知道最大值是多少? 如果我们有一个最大值出现两次的数组会有很大不同吗? {1,5,5,2,3} 非常感谢你。

EDIT1:我已经想出了如何为包含int maxIndex = 0的一个实例做到这一点;并在设置最大值的同时进行设置。 现在我只需要找出多个实例。

int[] score = new int[5];
    for (int i=0 ; i<=4 ;i++)
{
    System.out.println("enter Score");

    score[i] = keyb.nextInt();
}
System.out.println(Arrays.toString(score)); //need import java.util.Arrays;

int max = score[0];
int maxIndex = 0;
for (int i = 1 ;  i<=score.length-1 ; i++)
{
    if (score[i] > max)
        {max = score[i];
    maxIndex= i; }
}
System.out.println("The maximum is " +max); //this finds the maximum. Now say we want to remove the maximum (no matter what the position)..
System.out.println("it is located at index " + maxIndex);

3 个答案:

答案 0 :(得分:0)

对于多个实例,您可以使用Set来跟踪数组中与最大数字对应的索引。

    int max = score[0];
    Set<Integer> maxIndices = new HashSet<>(); // a set that contains the indices of the max number in the array
    maxIndices.add(0);

    for (int i = 1; i <= score.length - 1; i++) {
        if (score[i] > max) {
            max = score[i];
            maxIndices.clear(); // clear the set as we have a new max number
            maxIndices.add(i);
        } else if (score[i] == max) {
            maxIndices.add(i); // keep track of  all the indices in the array that corresponds to the max number
        }
    }

    // create the new array with the new size
    int newArrayWithoutMaxNums[] = new int[score.length - maxIndices.size()];
    int newCounter = 0;
    for (int i = 0; i < score.length; ++i) {
        if (!maxIndices.contains(i)) { // determine if the score is the max
            newArrayWithoutMaxNums[newCounter++] = score[i];
        }
    }

    for (int i = 0; i < newArrayWithoutMaxNums.length; ++i) {
        System.out.print(newArrayWithoutMaxNums[i] + "\t");
    }

答案 1 :(得分:0)

您可以尝试这样的事情:

public class findMaxIndex {

public static void main(String[] args) {

int[] score = new int[]{1,5,5,5,5,2,4,6,6,6,6,1,4,1};

int max = score[0];
int[] maxIndexArray = new int[score.length];
int j = 0;
for (int i = 1;  i <= score.length-1 ; i++) {
    if (score[i] > max) {
        max = score[i];
        j = 0;
        maxIndexArray = new int[score.length];
        maxIndexArray[j++] = i;
    }
    else if (score[i] == max) {
        maxIndexArray[j++] = i;
    }
}
System.out.println("The maximum is " +max); //this finds the maximum. Now say we want to remove the maximum (no matter what the position)..
System.out.println("it is located at index ");
 for (int i = 0; i < maxIndexArray.length - 1; i++) {
    System.out.println(maxIndexArray[i]);
 }
}

}

答案 2 :(得分:0)

你可以做更好的事情。但是,我认为至少有一个简单的例子是好的。因此,下面,虽然我当然建议尝试找出其他答案,即使使用的方法是你还没有学到的东西。

让我们先稍微改变你的代码。

int max = score[0];
int maxIndex[] = new int[score.length]
int maxCount = 0;
for (int i = 1 ;  i < score.length ; i++){
    if (score[i] > max){
        max = score[i];
        maxCount = 0;
        maxIndex[maxCount++] = i;
    }
    else if (score[i] == max)
    {
        //maxCount++ performs maxCount = maxCount+1; after the specified operation
        maxIndex[maxCount++] = i;
    }
}
System.out.println("The maximum is " + max);
System.out.print("This value occurs at: ");
for(int i = 0; i < maxCount; i++){
    System.out.print(maxIndex[i]);
}