I am trying to copy an index of a 2 dimensional array to another index of the same array.
MY CODE:
for (int f=0; f<(wp[donsay-1].length); f++) {
wp[donsay][f] = wp[donsay - 1][f];
}
With this code I am desperatly trying to copy the previous index to the next index. donsay=1 and the wp[0][n]... has been filled manually.
This gives the following error:
Shutting down VM
12-27 04:37:04.610 26200-26200/chessactivesekizsatirvetekliarray.com.sekizsatirvetekliarray E/AndroidRuntime: FATAL EXCEPTION: main
Process: chessactivesekizsatirvetekliarray.com.sekizsatirvetekliarray, PID: 26200
java.lang.RuntimeException: Unable to start activity ComponentInfo{chessactivesekizsatirvetekliarray.com.sekizsatirvetekliarray/chessactivesekizsatirvetekliarray.com.sekizsatirvetekliarray.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
Any help will be much aprreciated.
ArrayIndexOutOfBoundsException means you're trying to access an index of the array that doesn't exist. Check that you can access things such as wp[donsay-1], wp[donsay][f] and wp[donsay - 1][f] by stepping through your code in debug mode.
I'm also not sure how the for loop is meant to work for a 2d array, since you essentially need a for loop within a for loop to go over the whole array
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With