I am trying to initialize a 3D multidimensional array in Java, and assign 10001 to every value in it using three 'for-loops', but I am getting a error.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6
I need help in understanding what I am doing wrong in this.
public static int maxProfit(int[] prices) {
int[][][] dp = new int[prices.length][2][2]; // Intitialize 3D array
for(int i = 0; i<= prices.length; i++){
for(int j = 0; j <= 2; j++){
for(int k = 0; k<= 2; k++){
dp[i][j][k] = 10001;
}
}
}
I use two 'for-loops' for initializing and assigning values in 2D array. I am not able to understand why is the same not working for 3D arrays