[转载自百度文库]数组拷贝(System.arraycopy,深度拷贝)--数组

数组拷贝(System.arraycopy,深度拷贝)--数组

ITjob课程资料】

拷贝数组

数组一旦创建后,其大小不可调整。然而,你可使用相同的引用变量来引用一个全新的数组:

int[] myArray = new int [6];

myArray = new int[10];

在这种情况下,第一个数组被丢弃,除非对它的其它引用保留在其它地方。

Java编程语言在System类中提供了一种特殊方法拷贝数组,该方法被称作arraycopy()。例如,araycopy可作如下使用:

int[] myArray = { 1, 2, 3, 4, 5, 6 };    // 原始数组

int[] hold = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };    // 新的更大的数组

 

System.arraycopy(myArray, 0, hold, 0, myArray.length); // 从没有Array拷贝所有元素到hold,从下标0开始

这时,数组hold有如下内容:1,2,3,4,5,6,4,3,2,1。

注意:如果数组保存的是基本类型的话直接把值拷贝过来。如果数组存放的是引用类型(类类型,数组类型(多维数组的拷贝)等),那么拷贝的是引用类型的地址,请看下面的例子:

5

class AA {

   int i;

 

   public AA(int ii) {

      i = ii;

   }

}

 

public class TestArrayCopy {

   public static void main(String[] args) {

      // 声明数组并初始化,源数组

      AA str1[] = new AA[] { new AA(1), new AA(2), new AA(3), new AA(4) };

      // 拷贝的目的数组

      AA str2[] = new AA[str1.length];

      // 完全拷贝,array方法参数的介绍看api

      System.arraycopy(str1, 0, str2, 0, str1.length);

      // 改变目的数组

      str2[1].i = 5;

      // 打印原始数组,如果没有改变说明是两个数组

      for (int i = 0; i < str1.length; i++) {

         System.out.print(str1[i].i + "  ");

      }

     

   }

}

                       

显然,数组发生了改变,也就是经过拷贝操作后,原始的数组和新拷贝的数组没有分离,因为所拷贝的将是元素的引用。

对于多维数组,由于数组本身是引用类型,所以其拷贝特性与引用类型数组相同。

6

public class TestMutipleDemensionArrayCopy {

   public static void main(String[] args) {

      // 定义数组的数组

      int[][] source = new int[5][];

 

      // 定义目的数组

      int[][] target1 = new int[5][];

      int[][] target2 = new int[5][];

 

      // 给源数组赋值

      for (int i = 0; i < 5; i++) {

         source[i] = new int[i + 1];

         // int temp=i;

         for (int j = 0; j < source[i].length; j++)

            source[i][j] = j + 1;

      }

      // 打印源数组的数据

      System.out.println("-------------源数据-------------");

      for (int i = 0; i < source.length; i++) {

         for (int j = 0; j < source[i].length; j++)

            System.out.print(source[i][j] + "   ");

         System.out.println();

      }

      // 数组的拷贝(浅拷贝)

      System.arraycopy(source, 0, target1, 0, source.length);

      // 改变目的1数组的值

      target1[1][0] = 100;

      // 打印源数组的信息,可以看到值改变,说明没有深拷贝

      System.out.println("-----------浅拷贝后输出-----------");

      for (int i = 0; i < source.length; i++) {

         for (int j = 0; j < source[i].length; j++)

            System.out.print(source[i][j] + "   ");

         System.out.println();

      }

 

      // 数组的深拷贝,先拷贝”第一维“的

      System.arraycopy(source, 0, target2, 0, source.length);

      // 再深拷贝

      for (int i = 0; i < 5; i++) {

         target2[i] = new int[i + 1];

         System.arraycopy(source[i], 0, target2[i], 0, i + 1);

      }

      // 改变目的2数组的数据

      target2[1][0] = 999;

      // 打印源数组的信息,可以看到值没有改变,说明是深拷贝

      System.out.println("-----------深拷贝后输出未把100改成999-----------");

      for (int i = 0; i < source.length; i++) {

         for (int j = 0; j < source[i].length; j++)

            System.out.print(source[i][j] + "   ");

         System.out.println();

      }

   }

}

转载于:https://www.cnblogs.com/danscarlett/p/4522023.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值