简体   繁体   English

如何显示数组中元素的位置?

[英]How would I display the position of an element in an array?

My dilemma is after the user inputs a number, that number is then checked to see if it's in the array, if it is i'll let them know that is in the array along with the position of that said number. 我的难题是在用户输入一个数字之后,然后检查该数字以查看它是否在数组中,如果是,我会让他们知道该数字在数组中以及该数字的位置。 I have it to where it prompts user for the number, but after that i get the ArrayIndexOutOfBoundsException error. 我有它提示用户输入数字的位置,但是在那之后我得到了ArrayIndexOutOfBoundsException错误。

Here's what I have so far: 这是我到目前为止的内容:

int [] iGrades = new int [30];

      System.out.print ("Enter the grades, when you're done input (-1) ");
      for (int i=0; i<iGrades.length;i++)
      {
          iGrades [i]= kb.nextInt();
          if (iGrades [i]< 0)
          {
              break;
          }
      }
      System.out.print ("\nEnter a grade to check if it's in the array");
      iVal = kb.nextInt();
      for(int i=0; i<=iGrades.length; ++i)
       {
            if(iVal == (iGrades[i]))
            {
                found = true;
                for(int j=0; j<=iGrades.length; ++j)
                {
                    iGrades[j]=j+1;
                }
                break;
            }

       }

      if (found == true)
      {

         System.out.println(iVal + " is in the array at position ");
      }
      else
      {
         System.out.println(iVal + " is NOT in the array.");
      }
   }   

Any assistance would be great. 任何帮助都会很棒。

The problem is in your second for loop. 问题出在第二个for循环中。 This 这个

      for(int i=0; i<=iGrades.length; ++i)

Change the <= to < . <=更改为<

The exception says it already. 异常已经说明了。 You are checking an index that is not in the bounds of the array any more. 您正在检查不在数组范围内的索引。 Look at the second for loop: 查看第二个for循环:

for(int i=0; i<=iGrades.length; ++i)

It runs from 0 to 30. The array only goes from 0 to 29 though. 它的范围是0到30。不过,数组只能是0到29。 You have to use < instead here: 您必须在此处使用<代替:

for(int i=0; i < iGrades.length; i++)

Since arrays are zero-based, this 由于数组是从零开始的,因此

i<=iGrades.length

will allow i to equal Grades.length , which is one past the last index of the array. 将允许i等于Grades.length ,它比数组的最后一个索引Grades.length一个。 Use < . 使用<

Remember 记得

Array Index start from 0 to length of array -1 数组索引从0 to length of array -1开始0 to length of array -1

Change the loop accordingly 相应地更改循环

Hint change <= to < 提示更改<=<

暂无
暂无

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

相关问题 我想在文本字段上显示数组的所有6个元素 - I would like to display all the 6 element of the array on the textfield 如何将JSONObject的元素转换为单个元素数组? - How would I convert an element of a JSONObject to a single element array? 我如何找到此数组索引的位置以及最小值? - How would i find the position of this array index and also the minimum? 如何显示数组的位置 - how to display the position of array 如何创建一个方法,将参数值存储在位置变量索引处的数组中,然后将 1 添加到位置变量 - How would I create a method that stores the parameter value in an array at the index of the position variable, then adds 1 to the position variable 我将如何遍历数组以查看用户输入变量(数字)是否等于数组中的一个元素并用“@”替换该元素 - how would i loop through an array to see if the user input variable(number) is equal to a element in the array and replace that element with a “@” 用户在数组中输入 10 个数字我将如何编写代码来查找该数字并输出它在数组中的位置? - user enters 10 numbers into an array how would I write code to find that number and output what position it is in the array? 如何在给定位置的数组中放置元素? - How can i place an element in an array on a given position? 如何在Java中显示数组的某个元素? - How do I display a certain element of an array in Java? 如何在 libgdx 中显示 map? - How would I display a map in libgdx?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM