数组拷贝---System.arraycopy()

  1. System.arraycopy()
arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

param:
src: the source array.
srcPos: starting position in the source array.
dest: the destination array.
destPos: starting position in the destination data.
length: the number of array elements to be copied.

  1. 代码
public class TestCopy {
    
    
    public static void main(String[] args) {
    
    
        int[] array1 = {
    
    1, 0, 0, 0, 1};
        int[] array2 = new int[array1.length+2];
        array2[0]=0;
        array2[array1.length+1]=0;
        System.arraycopy(array1,0,array2,1,array1.length);
        System.out.println(Arrays.toString(array2));  //[0, 1, 0, 0, 0, 1, 0]
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_51681634/article/details/112251928