如何获取数组中相同大数的多个索引?

huangapple go评论74阅读模式
英文:

How to get the multiple index of the same large numbers in an array?

问题

我正在尝试获取数组中较大数值的索引,但我在这方面遇到了问题。
如果数组中只有一个较大的数,我的代码是有效的。然而,如果数组中有一个或多个相同的较大数,代码就不起作用。

例如,

  • 如果我有一个数组{2,1,1,2,1},它应该返回索引0和3(但实际上不会返回)
  • 如果我有一个数组{2,1,3,2,1},它会返回索引2。(这个是有效的)

目前,我试图通过使用数组来存储索引,但是在我的代码中,如我上面的例子所示,它只会返回找到的第一个较大数的索引。

我的代码:

class Main {

public static void getIndexOfMax(int array[]) {
    int max = array[0];
    int pos = 0;
    int max_index[] = new int[array.length];
    int counter = 0;

    for(int i=1; i<array.length; i++) {
        if (max < array[i])
        {
            pos = i;
            max = array[i];
            max_index[counter] = pos;
            counter += 1;
        }
    }

public static void main(String[] args) {
    int[] num = {2,1,1,2,1};
    getIndexOfMax(num);
  }
}

提前感谢您的任何回复!

英文:

I am trying to get the indices of large numbers within an array and I am having trouble doing so..
My code works if there is only one large number in the array. However, if there is one or more of the same large number, it does not works.

For example,

  • if I have an array {2,1,1,2,1}, it should return me the index 0 and 3 (this does not)
  • if I have an array {2,1,3,2,1}, it will then return me index 2. (this works)

Currently, I am trying to store the index by making use of an array, however in my code, as given in my example above, it only returns me the index of the first large number it found.

My code:

class Main {

public static void getIndexOfMax(int array[]) {
    int max = array[0];
    int pos = 0;
    int max_index[] = new int[array.length];
    int counter = 0;

    for(int i=1; i&lt;array.length; i++) {
        if (max &lt; array[i])
        {
            pos = i;
            max = array[i];
            max_index
0
+
网站访问量
= pos; counter += 1; } } public static void main(String[] args) { int[] num = {2,1,1,2,1}; getIndexOfMax(num); } }

Thanks in advance for any replies!

答案1

得分: 1

你可以再加一个条件语句,如果最大值等于数组元素,则存储该元素的索引。然后你需要声明另一个数组来存储最大值的索引位置。

if (max < array[i]) {
    pos = i;
    indexStorage.add(pos);
    max = array[i];
    max_index[counter] = pos;
    counter += 1;
}
英文:

You can have another conditional stating, if max is equal to an array element, then store that element index also. Then you would have to declare another array for the max index positions.
`if (max < array[i]) {

pos = i;

indexStorage.add(pos)

max = array[I];

max_index

0
+
网站访问量
= pos;

counter += 1;
}`

答案2

得分: 1

你需要在再次找到最大值并存储索引时,检查另一种情况。当新的最大值重新开始计数时,

public static void printIndexOfMax(int array[]) {
  int max = 0;
  int max_index[] = new int[array.length];
  int counter = 0;

  for (int i = 0; i < array.length; i++) {
    if (max <= array[i]) { // 允许相同的最大值
      if (max < array[i])  // 当找到新的最大值时从0开始计数
          counter = 0;
      max = array[i];
      max_index[counter] = i;
      counter++;
    }
  }
  for (int i = 0; i < counter; i++) {
    System.out.println(max_index[i]);
  }
}
英文:

You need to check another case when you find the maximum value again and store the index. And when the new maximum value starts the counter from 0 again.

  public static void printIndexOfMax(int array[]) {
    int max = 0;
    int max_index[] = new int[array.length];
    int counter = 0;

    for (int i = 0; i &lt; array.length; i++) {
      if (max &lt;= array[i]) { // Allowing same maximum number
        if (max &lt; array[i])  // Start counter from 0 when new maximum value found
            counter = 0;
        max = array[i];
        max_index
0
+
网站访问量
= i; counter++; } } for (int i = 0; i &lt; counter; i++) { System.out.println(max_index[i]); } }

huangapple
  • 本文由 发表于 2020年10月15日 12:32:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64364915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定