I am new to working with multidimensional arrays, and seem to not be able to get my head around this problem:
I am currently trying to create some code which increases the dimensions of a multidimensional array. The aim is to take an array named flat array, and add newDimensions. In the case I have in my test, an array is created: [0,20,10,30], which is an array of dimensions (2,2) and newDimensionssize(2) is to be added.
Now my code below creates a new flatArray of [0,0,10,10,5,5,15,15], but my issue is that the test does not pass because it is expected to be [0,0,5,5,10,10,15,15].
Am I maybe not understanding how nested arrays work, or is there something else that could be useful to help me achieve the desired result?
Many thanks for your help in advance!
@Override
public void addDimension(int newDimensionSize) {
int[] arrayAddedDimensions = new int[flatArray.length * newDimensionSize];
int arrayValue;
int j = 0;
int k = 0;
for (int i = 0; (i < flatArray.length); i++) {
arrayValue = flatArray[i];
int newValue = (arrayValue / newDimensionSize);
for (k = 0; (k < newDimensionSize); k++) {
arrayAddedDimensions[j] = newValue;
j++;
}
}
flatArray = arrayAddedDimensions;
}