Java Arry Index Out of Bound Exception

懵懂的女人 提交于 2019-12-02 11:16:31

First, if you want 5 values in an array, then declare it with 5:

int numberArray[] = new int[5];

Second, you are going off the end of the array. Change

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

to

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

You'll need to change your other j for loop this way also.

As an aside, your getMin method needs another change besides the change I mentioned above, because saying num[j+1] will still run off the end of the array even if you make the change above. I think you'll need to compare the current array element versus lowestNum, not the next array element.

<= numberArray.length should become < numberArray.length

since arrays are 0 indexed.

Remove the = in both loop.

public static void main(String args[]){
   .......
   for (int i = 0; i < numberArray.length; i++){
       ........
   }
   .........
}

and

public static int getMin(int num[]){
 .....
   for (int j = 0; j < num.length -1 ; j++){

   }
   ....
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!