重新调整数组大小(如扩展和图像)- Java
抱歉,让我重新表述一下问题,
我需要弄清楚如何将数组的每个元素移动到新数组中:就像每个元素的“x”位置将移动到“x*2”和“x*2+1”:所以 array[0 ]-> array2[0]、array2[1] 和 array[1] -> array2[2]、array2[3] 等,但对于“y”值也
对于我的Java应用程序,我需要一个函数来输入
[[1,0,1],
[1,0,1],
[1,1,1]]
并复制数组并输出,
[[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]]
这是我能弄清楚的
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;
}
我的目标是复制每个元素在数组的右侧和下方
例如:
OriginalArray[0][0]
将被放入
NewArray[0][0]、NewArray[0][1]、NewArray[1][0]、NewArray[1][1]
谢谢
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 also
For 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,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]
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输出:
output: