美文网首页
合并数组

合并数组

作者: Kira_8768 | 来源:发表于2019-04-30 14:49 被阅读0次

    public class combineArray {
    /*
    1.准备2个数组,a长度为5,b长度为8,并随机填充数;
    2.准备数组c,长度是a.length+b.length;
    3.使用System.arraycopy(src,srcPos,dest,destPos,length)把a,b合并到c
    /
    public static void main(String[]args){
    int a[] = new int[5];
    int b[] = new int[8];
    int c[] = new int[a.length+b.length];
    //填充数组a,b
    System.out.print("数组a={");
    for(int i=0;i<a.length;i++){
    a[i] = (int)(Math.random()
    100);
    System.out.print(a[i]+",");
    }
    System.out.println("}");
    System.out.print("数组b={");
    for(int j=0;j<b.length;j++){
    b[j] = (int)(Math.random()
    100);
    System.out.print(b[j]+",");
    }
    System.out.println("}");
    System.arraycopy(a,0,c,0,a.length);
    System.arraycopy(b,0,c,a.length,b.length);
    System.out.print("数组c={");
    for(int each:c){
    System.out.print(each+",");
    }
    System.out.println("}");
    }
    }

    相关文章

      网友评论

          本文标题:合并数组

          本文链接:https://www.haomeiwen.com/subject/phkvnqtx.html