import java.util.Arrays;
public class MergeTwoArrays2 {
public static void main(String[] args) {
int a[] = { 10, 20, 30 };
int b[] = { 40, 50, 60, 70, 80 };
// determining length of both arrays
int a1 = a.length;
int b1 = b.length;
// resultant array size
int c1 = a1 + b1;
// Creating a new array
int[] c = new int[c1];
// Loop to store the elements of first
// array into resultant array
for (int i = 0; i < a1; i = i + 1) {
// Storing the elements in
// the resultant array
c[i] = a[i];
}
// Loop to concat the elements of second
// array into resultant array
for (int i = 0; i < b1; i = i + 1) {
// Storing the elements in the
// resultant array
c[a1 + i] = b[i];
}
System.out.println("" + Arrays.toString(c));
}
}
算法不进行合并排序。在您的情况下,它只是串联了两个阵列A []和B []。由于数组中的所有元素均已排序,因此您将获得所得的数组进行排序。如果您将随机数放入两者之间并打扰顺序,则将不再对结果进行排序。