有两个数组:
数组a:{7,12,4,9,6,3,9}
数组b:{2,6,3,54,2,56,21,33}
将两个数组合并为数组c,排序打印输出
代码如下:
package com.homework.lhh;
import java.util.Arrays;
public class Ex15 {
public static void main(String[] args) {
int[] a = { 7, 12, 4, 9, 6, 3, 9 };
int[] b = { 2, 6, 3, 54, 2, 56, 21, 33 };
int[] c = new int[a.length + b.length];
// 分别输出数组a和数组b
System.out.print("数组a为:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
System.out.print("数组b为:");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
System.out.println();
for (int i = 0; i < a.length; i++) {
c[i] = a[i];
}
int n = 0;
for (int i = a.length; i < c.length; i++) {
c[i] = b[n];
n++;
}
System.out.print("数组c为(数组a+数组b并且升序打印):");
Arrays.sort(c);
for (int i = 0; i < c.length; i++) {
System.out.print(c[i] + " ");
}
}
}
运行结果如图: