Sorry let me rephrase the question,
I need to figure out how to move each element of the array into the new array: like for the "x" position of each element would be moved to "x*2" and "x*2+1": so array[0] -> array2[0], array2[1] and array[1] -> array2[2], array2[3] etc. but for the "y" value alsoFor my Java application I need a function that inputs
[[1,0,1],
[1,0,1],
[1,1,1]]
And would replicate the array and output
[[1,1,0,0,1,1],
[1,1,0,0,1,1],
[1,1,0,0,1,1],
[1,1,0,0,1,1],
[1,1,1,1,1,1],
[1,1开发者_JS百科,1,1,1,1]]
here is what I can figure out
public short[][] expandArray(short[][] arr) {
short[][] newArray = new short[arr.length*2][arr[0].length*2];
for(int s=0; s<arr.length; s++)
for(int ss=0; ss<arr[0].length; ss++) {
newArray[s*2][(new corresponding row)] = arr[s][ss];
newArray[s*2+1][(new corresponding row)] = arr[s][ss];
newArray[s*2][(next row down)] = arr[s][ss];
newArray[s*2+1][(next row down)] = arr[s][ss];
}
return newArray;
}
My goal is to duplicate each element in the array to the right and down
EX: OriginalArray[0][0] would be put into NewArray[0][0], NewArray[0][1], NewArray[1][0], NewArray[1][1] Thanksimport java.util.Arrays;
public class Main
{
public static void main(String args[])
{
int[][] array = { {1,0,1}, {1,0,1}, {1,1,1} };
int[][] newArray = replicate(array);
int i = 1;
System.out.print("[ ");
for(int[] a : newArray)
{
System.out.print(Arrays.toString(a) + (i++ != newArray.length? ", " : "") );
}
System.out.println(" ]");
}
public static int[][] replicate(int[][] array)
{
int x = array.length;
int y = array[0].length;
int counterX = 0;
int counterY = 0;
int[][] newArray = new int[2 * x][2 * y];
for(int[] a : array)
{
for(int b : a)
{
newArray[counterX++][counterY] = b;
newArray[counterX--][counterY++] = b;
newArray[counterX++][counterY] = b;
newArray[counterX--][counterY++] = b;
}
counterY = 0;
counterX++;
counterX++;
}
return newArray;
}
}
output:
[ [1, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 1], [1, 1, 0, 0, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1] ]
精彩评论