我有两个数组,我喜欢以某种方式合并,所以我的输出应该是这样的
我们也可以选择多维数组吗?
public class MeregTwoArray {
public static int[] mergeArray(int[] a, int[] b) {
int length = (a.length + b.length);
int result[] = new int[length];
for (int i = 0; i <= a.length-1;) {
result[i] = a[i];
for (int j = 0; j <= b.length-1;) {
result[i + 1] = b[j];
j++;
break;
}
i++;
}
return result;
}
public static void main(String[] args) {
int a[] = {1, 3, 5, 6, 7, 8};
int b[] = {4, 2, 7, 6, 4, 2};
int result[] = mergeArray(a, b);
for (int i = 0; i <= result.length - 1; i++) {
System.out.println(result[i]);
}
}
}
电流输出:1 3 5 6 7 8 4 0 0 0 0 0
预期输出:
1 4 3 2 5 7 6 6 7 4 8 2
慕虎7371278
陪伴而非守候
相关分类