Java 检索数组最大值的索引

Java 检索数组最大值的索引,java,indexing,element,Java,Indexing,Element,假设我有一个元素为{1,5,2,3,4}的数组。 我有找到最大值的代码,它是5。 我希望通过将数组[1]替换为数组[2],将数组[2]替换为数组[3]等,然后使用更少的索引创建一个新数组(以避免重复上一个值),从而从数组中删除此值。 在知道最大值是什么的情况下,如何查找/声明数组最大值的索引? 如果我们有一个最大值出现两次的数组,会有很大的不同吗?{1,5,5,2,3} 多谢各位 EDIT1:我已经为一个包含int-maxIndex=0的实例找到了实现方法;并在设置最大值的同时设置它。 现在,我

假设我有一个元素为{1,5,2,3,4}的数组。 我有找到最大值的代码,它是5。 我希望通过将数组[1]替换为数组[2],将数组[2]替换为数组[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);
int[]分数=新的int[5];

对于(int i=0;i对于多个实例,可以使用一个集合来跟踪数组中对应于最大数量的索引

    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");
    }
int max=score[0];
Set maxIndexes=new HashSet();//包含数组中最大数的索引的集合
加上(0);
对于(int i=1;i max){
max=分数[i];
MaxIndexes.clear();//清除集合,因为我们有一个新的最大值
增加(i);
}否则如果(分数[i]==最大值){
maxIndexes.add(i);//跟踪数组中与最大数对应的所有索引
}
}
//创建具有新大小的新阵列
int newArrayWithoutMaxNums[]=新int[score.length-maxIndexes.size()];
int newCounter=0;
对于(int i=0;i
您可以尝试以下方法:

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]);
 }
}

}
公共类findMaxIndex{
公共静态void main(字符串[]args){
int[]分数=新的int[]{1,5,5,5,2,4,6,6,6,1,4,1};
int max=分数[0];
int[]maxIndexArray=新的int[score.length];
int j=0;
对于(int i=1;i max){
max=分数[i];
j=0;
maxIndexArray=新整数[score.length];
maxIndexArray[j++]=i;
}
否则如果(分数[i]==最大值){
maxIndexArray[j++]=i;
}
}
System.out.println(“最大值是”+max);//这会找到最大值。现在我们要删除最大值(无论位置如何)。。
System.out.println(“它位于索引中”);
对于(int i=0;i
您可以做一些更好的事情。但是,我认为至少有一个简单的例子是好的。因此,下面我当然建议您尝试找出其他答案,即使所使用的方法是您尚未学会的

让我们从稍微修改代码开始

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]);
}
int max=score[0];
int maxIndex[]=新的int[score.length]
int maxCount=0;
for(int i=1;i最大值){
max=分数[i];
最大计数=0;
maxIndex[maxCount++]=i;
}
否则如果(分数[i]==最大值)
{
//maxCount++在指定的操作之后执行maxCount=maxCount+1
maxIndex[maxCount++]=i;
}
}
System.out.println(“最大值为”+max);
System.out.print(“此值出现在:”);
对于(int i=0;i
向我们展示您尝试过的内容。是否会有所不同取决于您的要求和实现,查找索引应该与查找最大值相同。欢迎使用堆栈溢出。您不清楚您的要求:我们应该删除所有出现的最大值还是只删除一次?甚至不尝试使用google?很抱歉,我没有展示我所做的。我不知道从哪里开始解决这个问题。我问的更多的是一个一般性问题。如果可能出现一次,我可能能够单独找出如何处理所有出现的最大值(如果没有那么大的差异)。