数组的复制

复制数组的方法:

在JAVA里面,可以用复制语句“A=B”给基本类型的数据传递值,但是如果A,B是两个同类型的数组,复制就相当于将一个数组变量的引用传递给另一个数组;如果一个数组发生改变,那么引用同一数组的变量也要发生改变。


以下是归纳的JAVA中复制数组元素值的的方法:(深拷贝)

1。使用FOR循环,将数组的每个元素复制(需要将每个对象调用clone方法,才能实现真正的复制)

2。使用clone方法,得到数组的值,而不是引用

3。使用System.arraycopy(s,start1,t,start2,length)方法

(注意:s是原数组,t是目标数组,start1&start2是开始复制下标,length一般是s的长度,由于arraycopy方法不给目标数组分配内存空间,所以必需要先为t分配内存空间!)

注意:
1.上面方法中arraycopy效率较高。

2.  以上所说的拷贝数组的方法,只是针对一维数组,对于多维数组,要在每一维用以上方法进行复制才能实现复制数组元素的值而不是引用。

3.  clone 和 arraycopy对二维数组进行复制时,是浅拷贝, 即

Object[][] aa;
Object[][] bb = aa.clone();
//or bb=System.arraycopy(aa,0,bb, 0, bb.length);
则:
boolean b1 = ( aa[i] == bb[i] );  //true
boolean b2 = (aa[i][j] == bb[i][j]);  //true,  可见数组元素只复制了引用。新旧数组指向相同的内存地址。(不论对象数组,还是基本类型数组)

//以下是例程

public class Test {

       public static void main(String[] args) {
             
              /*
               * 对象的引用传递
               */
//              A a = new A();
//              B b = new B(a);
//           System.out.println( a.getName() );
//           System.out.println( b.getA().getName() );
//           System.out.println( a==b.getA() );
//           a.setName("KE");
//           System.out.println( b.getA().getName() );
             
              /**
               *  数组的浅拷贝是指数组拷贝时,只拷贝了数组的地址,新旧数组指向同一数据
               *  数组的深拷贝,不论数据是基本类型,还是对象类型,都是一样的。
               *  对数组来说,不一样的地方在于,当为数组元素赋值时,基本类型值传递,对象类型是引用传递。
               *
               */
              Object[] a = new Object[]{"String", new Integer(1)};
              Object[] b = a;
             
              /**
               * 数组深拷贝的方法有如下几种:
               *  1。 调用clone
               *  2。 调用System.arraycopy
               *  以上两种对基本类型和对象类型数据效果等同。
               *  3。 使用FOR循环,将数组的每个元素复制。(注意调用clone方法)
               *
               */
             
              /*
               * 当数组数据是基本类型时,
               */
//              int[] array = new int[]{0,1,2,3,4};
//              int[] copy = array.clone();                  //1.拷贝数据
//              System.out.println( copy.equals(array));
//              System.out.println( copy == array );
//              for (int i = 0; i < copy.length; i++) {
//                     System.out.print( copy[i]+", " );       
//                     copy[i]++;                               //2.改变新数组数据内容
//                     System.out.print( copy[i]+", " );          
//                     System.out.println( array[i]+",");       //3.不影响原始数组
//              }
//              System.out.println();
             
             
              /*
               * 当数组数据是对象类型时,
               */
//              Object[] src = new Object[]{  new String("Zhao"),
//                                                        Integer.valueOf(1),
//                                                        Integer.valueOf(2),
//                                                                     Integer.valueOf(3),
//                                                                     Integer.valueOf(4)};                           
//                           
//              Object[] dest = src.clone();                   //1.拷贝数据
//             
////              Object[] dest = new Object[5];
////              System.arraycopy(src, 0, dest, 0, dest.length);
//             
//              System.out.println( dest.equals(src));
//              System.out.println( dest == src );
//              for (int i = 0; i < dest.length; i++) {
//                     System.out.print( dest[i]+", " );       
//                     dest[i] = new String("KE");                  //2.改变新数组内容                    
//                     System.out.print( dest[i]+", " );          
//                     System.out.println( src[i]+",");             //3.不影响原始数组
//              }
//              System.out.println();
             
             
              /**
               * 对多维数组(多维基本类型数组和多维对象数组完全一致。)
               *
               */             
              //多维基本类型数组
              int[][] aa = new int[][]{
                            {1, 2, 3},
                            {4, 5, 6},
                            {7, 8, 9}
              };
             
//              //多维对象类型数组
//              Object[][] aa = new Object[][]{
//                            { Integer.valueOf(1),Integer.valueOf(2),Integer.valueOf(3) },
//                            { Integer.valueOf(4),Integer.valueOf(5),Integer.valueOf(6) },
//                            { Integer.valueOf(7),Integer.valueOf(8),Integer.valueOf(9) }
//              };
             
              /**
               * 一维数组下的深拷贝在 多维数组 只是浅拷贝!!
               */
              int[][] bb = aa.clone();                                    //一维数组下的深拷贝,对于二维数组只是浅拷贝!!
//              int[][] bb = new int[aa.length][aa[0].length];
//              System.arraycopy(aa, 0, bb, 0, aa.length);
             
//              Object[][] bb = aa.clone();
////              Object[][] bb = new Object[3][3];                                
////              System.arraycopy(aa, 0, bb, 0, aa.length);                  //一维数组下的深拷贝,对于二维数组只是浅拷贝!!
             
             
              /**
               * 二维数组的深拷贝的实现方式!!!  转为一维数组拷贝。
               */
//              for (int i = 0; i < bb.length; i++) {                       //实现深拷贝的方法!!!!!!!!!!!! 
//                     System.arraycopy(aa[i], 0, bb[i], 0, aa[i].length); 
//                    // bb[i] = aa[i].clone();
//              }
             
             
              System.out.println("## 初始 aa:" );                         //1. 初始原数组
              for (int i = 0; i < aa.length; i++) {
                     for (int j = 0; j < aa[i].length; j++) {
                            System.out.print(aa[i][j]+" ");
                     }
                     System.out.println( );
              }
             
              System.out.println("## bb = aa.clone() 后bb:" );           //2. 新数组(值等于原数组的值)
              for (int i = 0; i < bb.length; i++) {
                     for (int j = 0; j < bb[i].length; j++) {
                            System.out.print(bb[i][j]+" ");
                     }
                     System.out.println( );
              }
             
              System.out.println("## bb改变后:" );                       //3.改变新数组后
              for (int i = 0; i < bb.length; i++) {
                     for (int j = 0; j < bb[i].length; j++) {
                            bb[i][j] += 10;  //for 多维基本类型数组
//                            bb[i][j] = new String("Zhao"); //for 多维对象类型数组
                            System.out.print(bb[i][j]+" ");
                     }
                     System.out.println( );
              }
             
              System.out.println("## bb改变后, aa:" );                    //4.输出原数组
              for (int i = 0; i < aa.length; i++) {
                     for (int j = 0; j < aa[i].length; j++) {
                            System.out.print(aa[i][j]+" ");
                     }
                     System.out.println( );
              }
             
       }

}

class A{
       private String name = "zhao";
       public String getName(){
              return name;
       }
       public void setName(String name){
              this.name = name;
       }
}
class B{
       A a;
       B(A a){
              this.a = a;
       }
      
       public A getA(){
              return a;
       }
}

原文地址:https://www.cnblogs.com/lulu638/p/3990443.html