首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java中数组的合并有关问题

2012-12-20 
Java中数组的合并问题转自:http://www.cnblogs.com/superdev/archive/2009/04/30/1447020.htmlpublic stat

Java中数组的合并问题
转自:http://www.cnblogs.com/superdev/archive/2009/04/30/1447020.html

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String sourceStr1 = "啊啊啊啊";
    String sourceStr2 = "哦哦哦哦哦哦哦哦哦哦";
    byte[] sourceByte1 = sourceStr1.getBytes();
    byte[] sourceByte2 = sourceStr2.getBytes();   
    byte[] target = new byte[sourceByte1.length+sourceByte2.length];
    System.out.println(new String(sourceByte1));
    System.out.println(new String(sourceByte2));
    int len = sourceByte1.length + sourceByte2.length;
    System.arraycopy(sourceByte1, 0, target, 0, sourceByte1.length);
    System.arraycopy(sourceByte2, 0, target, sourceByte1.length, sourceByte2.length);
    System.out.println(new String(target));
    //这里打印:啊啊啊啊哦哦哦哦哦哦哦哦哦哦
}

这里主要用到了System.arraycopy方法。

System.arraycopy(src, srcPos, dest, destPos, length)

src : 源数组

srcPos:源数组中的需要复制的起始位置

dest:目标数组

desPos:目标数组中需要复制到的起始位置

length:需要复制源数组的长度

热点排行