查找数组中最大元素的索引

时间:2015-06-01 05:25:19

标签: java arrays indexing

我想编写一个返回给定数组中最大元素索引的代码。但是,当我尝试编译时,我收到的消息似乎无法理解。虽然我已经完成了错误消息所说的内容,但它一直告诉我修复它们。请帮助这个新手程序员!

public class largest {
    int[] array = new int[10];
    array[0] = 100;
    array[1] = 200;
    array[2] = 300;
    array[3] = 400;
    array[4] = 500;
    array[5] = 600;
    array[6] = 700;
    array[7] = 800;
    array[8] = 900;
    array[9] = 1000;
    int index;

    public static void main(String args) {

        for (int i=0; i<10; i++) {
            if (array[i] < array[i+1]) {
                index = i + 1;
            } else {
                index = i;
            }
            i++;
        }
        System.out.println(index);
    }
}

以下是编译信息。

largest.java:3: error: ']' expected
    array[0] = 100;
      ^
largest.java:3: error: ';' expected
    array[0] = 100;
       ^
largest.java:3: error: illegal start of type
    array[0] = 100;
         ^
largest.java:3: error: <identifier> expected
    array[0] = 100;
          ^

所有指数都在继续。

3 个答案:

答案 0 :(得分:4)

这种类型的初始化不能在方法之外完成:

array[0] = 100;
array[1] = 200;
array[2] = 300;
array[3] = 400;
array[4] = 500;
array[5] = 600;
array[6] = 700;
array[7] = 800;
array[8] = 900;
array[9] = 1000;

将作业移至您的主要变更

int[] array = new int[10];

int[] array = {100,200,300,400,500,600,700,800,900,1000};
顺便说一下,除了这个编译错误之外,你的代码还没有做它应该做的事情。它将抛出一个ArrayIndexOutOfBoundsException(因为当{9}达到9时,array[i+1]将超出界限。为了找到最大元素的索引,你必须保持一个保持当前最大值的变量,并将它与数组的所有元素进行比较。

public static void main(String args) {
    int max = array[0];
    index = 0;
    for (int i=1; i<10; i++) {
        if (array[i] > max) {
            index = i;
            max = array[i];
        }
    }
    System.out.println(index);
}

答案 1 :(得分:1)

几乎没有更正:

&#13;
&#13;
public class largest {
  //move inside main() from here
  int[] array = new int[10];
  array[0] = 100;
  array[1] = 200;
  array[2] = 300;
  array[3] = 400;
  array[4] = 500;
  array[5] = 600;
  array[6] = 700;
  array[7] = 800;
  array[8] = 900;
  array[9] = 1000;
  int index; //required default value
  //move inside main() till here

  public static void main(String args) { //missing [] symbol

    for (int i = 0; i < 10; i++) {
      if (array[i] < array[i + 1]) {
        index = i + 1;
      } else {
        index = i;
      }
      i++;
    }
    System.out.println(index);
  }
}
&#13;
&#13;
&#13;

您可以尝试这个例子:

&#13;
&#13;
public class largest {
  public static void main(String[] args) {
    int[] array = {
      345, 768, 95, 345, 678
    }; //array with default elements
    int index = -1, high = array[0], i = 0;
    for (int e: array) { //loop in every element
      if (e > high) { //check for highest value
        index = i; //assign index of the highest value till now
        high = e; //assign the highest value till now
      }
      i++; //next index
    }
    System.out.println(index); //-1 if not found
  }
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

public static void main(String[] args) {
    int[] array = {100, 200, 300, 400, 500, 600, 700, 800, 900};
    System.out.println(highestValue(array));
}

public static int highestValue(int[] arr) {

    int index = 0;
    int highestValue = Integer.MIN_VALUE;

    for(int i = 0; i < arr.length; i++) {
        int temp = arr[i];
        if(temp > highestValue) {
            highestValue = temp;
            index = i;
        }
    }
    return index;
}