int array[] = {0,0,0,0,0,0,0,0,0,0}
if (array[9] == 0) { //if the array is full, skip this
currentVal = array[i]; //store current value in array
i++;
} else {
//store max value of array in medHigh
for (int x = 0; x < 10; x++) {
if (array[x] > medHigh) {
medHigh = array[x];
}
}
}
Asked
Active
Viewed 126 times
-2

ΦXocę 웃 Пepeúpa ツ
- 47,427
- 17
- 69
- 97

Oliver Klingefjord
- 53
- 4
-
2What's `i`? What's its value? – Biffen Oct 12 '16 at 10:15
1 Answers
1
Simple because your array has length 10 so the maximum index is 9. Arrays in Java are 0-based. It means, that it's indexes are from 0 incl
to array.length - 1 incl
which in your case is 9
.
Java gave you a stacktrace. It looks like following (by the way, A L W A Y S attach it to the question about Exception
s):
java.lang.ArrayIndexOutOfBoundsException: 10
at package.ClassName.methodName(ClassName.java:lineNumber)
...
Is the line number the number of line in which you call currentVal = array[i];
? If yes, it's your answer - i
is 10
.
If you paste your stack trace
, comment my answer for further help. I'll answer with pleasure.

xenteros
- 15,586
- 12
- 56
- 91