数组的复制

来源:互联网 发布:tensorflow 在spark 编辑:程序博客网 时间:2024/05/22 13:26
public class test{
    public static void main (String [] args)
    {
        System.out.println("==========一维数组=====================");
        //一维数组
        String[] intArr= {"Mircosoft","IBM","Sun","Oracle","Apple"};
        String[] copyArr=new String[7];
        System.arraycopy(intArr,0,copyArr,2,intArr.length);  //要在源数组的0位置开始复制
        for (int i=0; i<copyArr.length;i++ ) 
        {
            System.out.print(copyArr[i]+"  ");
        }
        System.out.println();
        System.out.println("==========二维数组=====================");
        
        //二维数组
        int[][] arr = {{1,2},{1,2,3,5,3},{3,4},{2,78,98,0}};
        int[][] copy = new int[5][];
        System.arraycopy(arr,0,copy,0,arr.length);
       
        for(int i = 0;i<arr.length;i++){
            for(int j =0;j<arr[i].length;j++){
                System.out.print(arr[i][j]+"  "); 
            }
            //System.out.println();
        }
        System.out.println();
        //重新设置copy数组   ,两个数组都会改变,因为指向相同的内存地址
       
        copy[2][1]=100;
       
       for(int i=0;i<arr.length;i++){
            for(int j=0;j<arr[i].length;j++){
                System.out.print(arr[i][j]+"  ");
            }
       }
        
        
    }
}

0 0