Java将数组添加到数组特殊模式
我正在用Java开发一个游戏,我需要缓存一些基于位置的信息。如果我有以下数组:
int[][] array = {
{1, 2, 3},
{1, 2, 3},
{1, 2, 3}
};
然后我有另一个数组:
int[][] otherArray = {
{4, 5, 6},
{4, 5, 6},
{4, 5, 6}
};
现在我想用一种特殊的方式把它们结合起来。我想在array
的左边添加otherArray
。因此,结果如下所示:
int[][] combinedArray = {
{4, 5, 6, 1, 2, 3},
{4, 5, 6, 1, 2, 3},
{4, 5, 6, 1, 2, 3}
};
然后我有另一个组合阵列:
int[][] otherCombinedArray = {
{30, 17, 139, 65, 335, 99},
{50, 43, 57, 53, 423, 534},
{90, 67, 78, 24, 99, 67}
};
现在我想将它添加到原始组合数组的顶部。因此,最终结果如下所示:
int[][] finalCombinedArray = {
{30, 17, 139, 65, 335, 99},
{50, 43, 57, 53, 423, 534},
{90, 67, 78, 24, 99, 67},
{4, 5, 6, 1, 2, 3},
{4, 5, 6, 1, 2, 3},
{4, 5, 6, 1, 2, 3}
};
有人能告诉我一个好的库或内置的方法来做这件事吗?我还想指出,该方法的计算量不应该太大(比如多次遍历所有数组,并且不应该使用太多内存,比如80MB)
谢谢你的帮助
# 1 楼答案
Java本身不提供连接方法,但我们可以使用
System.arraycopy
(正如上面@user16320675所建议的)使用
System.arraycopy
指定要复制的数组+目标数组->;有大小为10的数组A和大小为2的数组B,可以使用命令将B数组复制到A中现在,如果我们使用
arraycopy
,似乎您必须确定何时作为行复制,何时作为列复制合并为行与另一行类似,但会更改要影响的位置以及创建合并数组的方式