你如何在Java中追加合并两个二维数组正常吗? [英] How do you append two 2D array in java properly?
本文介绍了你如何在Java中追加合并两个二维数组正常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直试图在java中后置两个2 D阵列。是否有可能得到一个例子,因为我一直在试图寻找它,但不能找到一个。
INT [] [] appendArray(空,窗)
{
INT [] []结果=新的INT [empty.length] [空[0] +。长度窗口[0]。长度]
}
解决方案
在这里你去:
进口java.util.Arrays中;
公共类Array2DAppend { 公共静态无效的主要(字串[] args){ INT [] []一个=新INT [] [] {{1,2},{3,4}};
INT [] [] B =新INT [] [] {{1,2,3},{3,4,5}}; 的System.out.println(Arrays.deepToString(一));
的System.out.println(Arrays.deepToString(B));
的System.out.println(Arrays.deepToString(追加(A,B))); } 公共静态INT [] []追加(INT [] []一,INT [] [] B){
INT [] []结果=新的INT [则为a.length + b.length个] [];
System.arraycopy(一,0,结果,0,则为a.length);
System.arraycopy(B,0,结果,则为a.length,b.length个);
返回结果;
}
}
和输出:
[1,2],[3,4]
[[1,2,3],[3,4,5]
[[1,2],[3,4],[1,2,3],[3,4,5]
I have been trying to append two 2 D arrays in java. Is it possible to get an example because I have been trying to look it up but cannot find one.
int [][]appendArray(empty,window)
{
int [][]result= new int [empty.length][empty[0].length+window[0].length];
}
解决方案
Here you go:
import java.util.Arrays;
public class Array2DAppend {
public static void main(String[] args) {
int[][] a = new int[][] {{1, 2}, {3, 4}};
int[][] b = new int[][] {{1, 2, 3}, {3, 4, 5}};
System.out.println(Arrays.deepToString(a));
System.out.println(Arrays.deepToString(b));
System.out.println(Arrays.deepToString(append(a, b)));
}
public static int[][] append(int[][] a, int[][] b) {
int[][] result = new int[a.length + b.length][];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
return result;
}
}
and the output:
[[1, 2], [3, 4]]
[[1, 2, 3], [3, 4, 5]]
[[1, 2], [3, 4], [1, 2, 3], [3, 4, 5]]
这篇关于你如何在Java中追加合并两个二维数组正常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文