java设计一个复制和打印一维数组的工具类

本期作业:

//设计一个复制和打印一维数组的工具类

class ArrayUtil{
    //数组复制方法
    public static void copyArray(int[]src,int[]dst){
        if (src == null || dst == null){
            return;
        }
        for (int index = 0; index < src.length && index < dst.length; index++){
            dst[index] = src[index];
        }
    }

    public static void printArray(int[]arr){
        for (int index = 0; index < arr.length; index++){
            if (index==0){
                System.out.println("[");
            }else{
                System.out.println(",");
            }
            System.out.println(arr[index]);
        }
        System.out.println("]");
    }
}
public class Class2_text {
    public static void main(String[] args) {
        int array1[] = {2,3,4,5,6,9};
        int array2[] = new int[6];
        ArrayUtil.copyArray(array1,array2);
        ArrayUtil.printArray(array2);
    }
}
Last modification:September 19th, 2019 at 11:17 pm

Leave a Comment