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]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With