0
点赞
收藏
分享

微信扫一扫

练习合并数组

海滨公园 2022-04-17 阅读 41
eclipsejava

package test;

public class 练习合并数组 {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        //创建两个数组,并将这两个数组合并到第三个数组中
        int [] a = new int [6];
        int [] b = new int [7];
        //对两个数组赋予随机数
        for(int i = 0;i<a.length;i++) {
            a[i] = (int)(Math.random()*100);
        }
        for(int j = 0;j<b.length;j++) {
            b[j] = (int)(Math.random()*100);
        }
        //创建的第三个数组
        int [] c = new int [13];
        //输出a[]    数组,等下作比较
        for(int i = 0;i<a.length;i++) {
            System.out.print(a[i]+" ");
            
        }
        System.out.println();
        //输出b[]    数组,等下作比较
        for(int j = 0;j<b.length;j++) {
            System.out.print(b[j]+" ");
            
        }
        System.out.println();
        //合并数组,首先将a数组合并到c数组中
        System.arraycopy(a, 0,c,0, 6);
        //将b数组合并到c数组中
        System.arraycopy(b, 0,c,6,7);
        //打印c数组
        for(int i = 0;i<c.length;i++) {
            System.out.print(c[i]+" ");
        }
    }

}

 

 

 

 

举报

相关推荐

0 条评论