I want to create a multi-dimensional array from a single dimensional array in java.
my single dimenstional array is like
int[] grid = {5, 3, 1, 2, 0, 4, 1, 1, 3 };
I want to create a matrix of 3x3 diminsions at runtime as there are 9 elements in this array. Can anyone suggest some good idea for this. Can anyone suggest me what should I do in below code so that it can give me arraylist of 3 arrays or if someone can suggust me some better idea to perform this correctly. I know there is error in my below code.
ArrayList<int[]> matrix = new ArrayList<>();
int[] tempArray = new int[n];
int j = 0;
for(int i=0 ; i<=grid.length; i++){
if((i+1) / 3 == 0){
matrix.add(tempArray);
j=0;
}else{
tempArray[j] = grid[i];
j++;
}
}
Thanks