ArrayCopyDemo方法

用ArrayCopyDemo方法可将源数组复制到目的数组中,如果目的代码小于源数组的长度,则抛出异常

package book;

public class ArrayCopyDemo {

    public static void main(String[] args) {
        int [] a={1,2,3,4,5,6,7,8};
        int [] a1={12,11,10,9,8,7,6,5,4,3,2,1};
        int [] a2={8,7,6,5,4};
        //抛出异常
        try{
            System.arraycopy(a, 0, a1, 0, a.length);
            System.arraycopy(a, 0, a2, 0, a.length);
        }catch(ArrayIndexOutOfBoundsException e){
            System.out.println(e);
        }
        for(int elem:a1)
        {
            System.out.print(elem+ " ");
        }
        System.out.println();
        for(int elem:a2)
        {
            System.out.print(elem+" ");
        }
        System.out.println("\n");
    }

}

你可能感兴趣的:(java,代码家)