Java for loops not populating array correctly -
i have following code:
public class solutionstest { public static void main(string[] args) throws filenotfoundexception, unsupportedencodingexception { int allsolutions[][][][][] = new int[16][16][16][16][16]; (int = 0; <= 15; i++) { (int j = 0; j <= 15; j++) { (int k = 0; k <= 15; k++) { (int l = 0; l <= 15; l++) { allsolutions[i][j][k][l][j] = i; system.out.println("set index " + + " " + j + " " + k + " " + l + " " + j + " equal " + i); } } } } system.out.println(allsolutions[4][2][1][4][5]); system.out.println(allsolutions[5][2][1][4][5]); system.out.println(allsolutions[6][2][1][4][5]); system.out.println(allsolutions[7][2][1][4][5]); system.out.println(allsolutions[8][2][1][4][5]); } }
the println check inside loop correctly reports stored data correctly, can see if run program. however, after loops, if try retrieve of values set inside loops, of values = 0. missing?
as set in loop, values should correspond index of first dimension of array such:
allsolutions[0][x][x][x][x] = 0;
allsolutions[1][x][x][x][x] = 1;
allsolutions[2][x][x][x][x] = 2;
and on...
you never assign allsolutions[4][2][1][4][5]
, or of other 4 array positions printing, remains 0. have 4 nested loops , 5 dimensions in array.
you assign values positions 2nd index equal 5th index. if try print, example, system.out.println(allsolutions[4][2][1][4][2]);
, you'll see non-zero value.
you should use 5 nested loop instead of re-using j index :
for(int i=0;i<=15;i++){ for(int j=0;j<=15;j++){ for(int k=0;k<=15;k++){ for(int l=0;l<=15;l++){ for(int m=0;m<=15;m++){ allsolutions[i][j][k][l][m] = i; system.out.println("set index "+ + " " + j + " " + k + " " + l + " " + m + " equal " + i); } } } } }
Comments
Post a Comment