この方法の一つ:
// SRC:ソース配列
// srcPos:データ・ソース・アレイコピーする初期位置から
標的配列:// DEST
// destPos:宛先アレイへコピー開始位置
//長さ:長さコピー
//System.arraycopyを( SRC、srcPos、DEST、destPos、長さ);
public static void main(String[] args) {
String str="";
String str1="";
byte[] codeStr =new byte[4];
byte[] codeStr251 = {1,2};
//src: 源数组
//srcPos: 从源数组复制数据的起始位置
//dest: 目标数组
//destPos: 复制到目标数组的起始位置
//length: 复制的长度
//System.arraycopy(src, srcPos, dest, destPos, length);
System.out.println("copy前--"+codeStr.length);
for(int i = 0;i<codeStr.length;i++){
str=str.concat("\r\n--第"+i+"个--:"+codeStr[i]);
}
System.out.println(str);
System.out.println("----------------------------------");
System.arraycopy(codeStr251, 0, codeStr, 0, 2);
System.out.println("copy后--"+codeStr.length);
for(int i = 0;i<codeStr.length;i++){
str1=str1.concat("\r\n--第"+i+"个--:"+codeStr[i]);
}
System.out.println(str1);
}
結果:
方法2:
forループ
public static void main(String[] args) {
byte[] code =new byte[4];
System.out.println("copy前--:"+Arrays.toString(code));
byte[] codeStr2 = {1,2};
for (int i = 0; i < codeStr2.length; i++){
code[i] = codeStr2[i];
}
System.out.println("copy后--:"+Arrays.toString(code));
}
結果: