while studying Array part in Java, I ran into this problem, and I searched about the error message,
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at Main.main(Main.java:12)"
and read some articles about it.
I got that the index start from 0 so it will end in n-1(n is the allocated size)
.But I still don't catch the problem in my code. It will mean a lot to me if you help me with it since array is kind of confusing part..
I need to return output :
1
3
5
7
9
2
4
6
8
10
And this is what I wrote.
import java.io.*;
class Main {
public static void main(String[] args) throws Exception {
int [] oddArray = new int[5];
int [] evenArray = new int[5];
int k = 0;
do {
k++;
oddArray[k] = k + 1;
evenArray[k] = k + 2;
}while(k <= 10);
for(int j = 0 ; j < 5 ; j++) {
System.out.println(oddArray);
}
for(int j = 0 ; j < 5 ; j++) {
System.out.println(evenArray);
}
}
}