Sử dụng giải pháp từ task, mà Ngài đã đề cập trong câu hỏi. Ví dụ:
import java.util.Arrays;
public class ArrayProgram {
public static void main(String[] args) {
int[][] array1 = { { 1, 2, 3 }, { 1, 2, 3 }, { 1, 2, 3 } };
int[][] array2 = { { 4, 5, 6 }, { 7, 8, 9 }, { 0, 1, 2 } };
int[][] result = ArrayCopier.joinSecondDimension(array1, array2);
for (int[] array : result) {
System.out.println(Arrays.toString(array));
}
}
}
class ArrayCopier {
public static int[][] joinSecondDimension(int[][] array1, int[][] array2) {
int[][] array1and2 = new int[array1.length][];
for (int index = 0; index < array1.length; index++) {
array1and2[index] = join(array1[index], array2[index]);
}
return array1and2;
}
public static int[] join(int[] array1, int[] array2) {
int[] array1and2 = new int[array1.length + array2.length];
System.arraycopy(array1, 0, array1and2, 0, array1.length);
System.arraycopy(array2, 0, array1and2, array1.length, array2.length);
return array1and2;
}
}
Prints:
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 7, 8, 9]
[1, 2, 3, 0, 1, 2]
EDIT
thực hiện đối với bất kỳ số lượng đối số (Variable-Length Argument Lists):
import java.util.Arrays;
public class ArrayProgram {
public static void main(String[] args) {
int[][] array1 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int[][] array2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int[][] array3 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
test(array1);
test(array1, array2);
test(array1, array2, array3);
}
private static void test(int[][]... arrays) {
int[][] result = ArrayCopier.joinSecondDimension(arrays);
for (int[] array : result) {
System.out.println(Arrays.toString(array));
}
System.out.println();
}
}
class ArrayCopier {
public static int[][] joinSecondDimension(int[][]... arrays) {
int firstArrayLength = arrays[0].length;
int[][] result = new int[firstArrayLength][];
for (int index = 0; index < firstArrayLength; index++) {
result[index] = join(getSecondDimArrays(index, arrays));
}
return result;
}
public static int[] join(int[]... arrays) {
int[] result = new int[getTotalLength(arrays)];
int destPos = 0;
for (int[] array : arrays) {
System.arraycopy(array, 0, result, destPos, array.length);
destPos += array.length;
}
return result;
}
private static int getTotalLength(int[]... arrays) {
int length = 0;
for (int[] array : arrays) {
length += array.length;
}
return length;
}
private static int[][] getSecondDimArrays(int index, int[][]... arrays) {
int[][] result = new int[arrays.length][];
int resultIndex = 0;
for (int[][] array : arrays) {
result[resultIndex++] = array[index];
}
return result;
}
}
Prints:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[1, 2, 3, 1, 2, 3]
[4, 5, 6, 4, 5, 6]
[7, 8, 9, 7, 8, 9]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
[4, 5, 6, 4, 5, 6, 4, 5, 6]
[7, 8, 9, 7, 8, 9, 7, 8, 9]
Chỉ cần sử dụng 'for' loop và sử dụng' arraycopy' bên trong nó. Bạn đã thử chúng chưa? –
tưởng tượng thứ nguyên thứ hai của mảng của bạn là mảng một chiều mà bạn muốn kết hợp. Vì vậy, lặp qua kích thước đầu tiên và làm những thứ mà bạn đã tìm thấy với chiều thứ hai. – timaschew
Vì có hai chiều, không rõ ràng nên sáp nhập như thế nào. Một ví dụ sẽ là tốt. –