java 两byte[] 合并

来源:互联网 发布:淘宝规则大全 编辑:程序博客网 时间:2024/05/30 13:42
package com.ly.core;


import java.io.UnsupportedEncodingException;
/**
 * 
 * @author luyou
 *
 */
public class Test {


public static void main(String[] args){
//准备两个数组first、second
byte[] first ={'a','b','c'};
byte[] second ={'d','e','f'};
//创建一个first+second长度的result空数组
byte[] result =new byte[first.length+second.length];
//将first数组填充到result中
System.arraycopy(first,0,result,0,first.length);
//将second数组填充到result中
System.arraycopy(second,0,result,first.length,second.length);
String s = "";
try {
s = new String(result,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//打印出结果,应为:“abcdef”
System.err.println(s);
}
/**
* static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 
          从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。 
*/


}
0 0