{{1, 2, 3},
{4, 5, 6}}
我想要做的是将2d数组的大小相乘,使得得到的数组每个元素重复x次(最好是32次)。示例最终结果:
{{1, 1, 2, 2, 3, 3},
{1, 1, 2, 2, 3, 3},
{4, 4, 5, 5, 6, 6},
{4, 4, 5, 5, 6, 6}}
在这种情况下,原始数字乘以2,因此每个值都会产生2乘2的正方形。有什么想法吗?
答案 0 :(得分:1)
假设您使用int:
int x = //...
int[][] input = //...
int[][] result = new int[input.length * x][];
final int size = input[0].length;
for (int i = 0; i < input.length; i++) {
int[] inputArray = input[i];
int[] array = new int[size * x];
final int offset = i * x;
result[offset] = array;
// fill first of the equal lines
for (int j = 0; j < size; j++) {
final int offset2 = j * x;
// copy every element x times
Arrays.fill(array, offset2, offset2 + x, inputArray[j]);
}
// copy line x-1 times
for (int j = 1; j < x; j++) {
// if you needn't write to the result, use array as right hand side instead
result[offset + j] = Arrays.copyOf(array, array.length);
}
}