Java-两个数组合并,并排序打印

有两个数组:
数组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] + " ");
        }
    }
}

运行结果如图:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值