java an array to another array inside copy

method one:

// src: the source array
        // srcPos: from an initial position to copy the data source array
        // dest: target array
        // destPos: copy start position to the destination array
        // length: length copy
        //System.arraycopy ( src, srcPos, dest, destPos, length);

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);			
	}		

result:

Method Two:

for loop

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));
	}	

result:

 

 

Published 141 original articles · won praise 33 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_43560721/article/details/103011222