This program is supposed to scale an array holding objects in each cell by a factor of two. For example, the array:
[[Object1,Object2],
[Object3,Object4]]
should become:
[[Object1,Object1,Object2,Object2],
[Object1,Object1,Object2,Object2],
[Object3,Object3,Object4,Object4],
[Object3,Object3,Object4,Object4]]
This is the code I have for the program so far:
public Color[][] doOperation(Color[][] imageArray)
{
int multiplier = 2;
Color[][] newArray = new Color[imageArray.length*2][imageArray[0].length*2];
for(int i = 0; i < imageArray.length; i++)
for(int j = 0; j < imageArray[0].length; j++)
{
newArray[i][j] = imageArray[i/multiplier][j/multiplier];
}
}
How should I change this code so that the new array is scaled properly? Any help is appreciated.
You mixed up your newArray
and imageArray
. You initialize double-sized newArray
(this is right) but then iterate over it, using imageArray
length. Just swap imageArray.length
to newArray.length
. And don't forget about return
;)
public static Color[][] doOperation(Color[][] imageArray) {
int multiplier = 2;
Color[][] newArray = new Color[imageArray.length*2][imageArray[0].length*2];
for(int i = 0; i < newArray.length; i++)
for(int j = 0; j < newArray[0].length; j++) {
newArray[i][j] = imageArray[i/multiplier][j/multiplier];
}
return newArray;
}