Two dimensional array depth cloning

Source: Internet
Author: User

Package cn.com.csuinfo.deepclone;

public class Testclone {
public static void Main (string[] args) {
Int[][] arr1 = {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}};
Int[][] ARR2;
Int[][] ARR3 ={{1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 41, 1}};
Int[] A = {1, 2, 3, 4};
Int[] A1;

Deepclone DC1 = new Deepclone ();
Deepclone DC2 = new Deepclone ();
Deepclone DC3 = new Deepclone ();
Dc1.setarr (ARR1);
Dc3.setarr (ARR3);
Dc1.seta (a);

try {
ARR2 = Dc1.clone (). Getarr (); Cloning arr1 to ARR2;
A1 = Dc1.clone (). Geta ();
DC2 = Dc1.clone ();
System.out.println ("Before arr1[0][1]:" + arr1[0][1]);
ARR1[0][1] = 20;
System.out.println ("After arr1[0][1]:" + arr1[0][1]);
System.out.println ("After arr12[0][1]:" + arr12[0][1]);
System.out.println ("Before a[1]:" + a[1]);
A1[1] = 22;
System.out.println ("After a[1]:" + a[1]);

/* Printed the source array * *
System.out.println ("arr1:");
For (int[] ar:arr1) {
for (int aa:ar) {
System.out.print (AA + ",");
}
System.out.println ();
}

System.out.println ("ARR2:");
For (int[] AR:DC2.GETARR ()) {
for (int aa:ar) {
System.out.print (AA + ",");
}
System.out.println ();
}

System.out.println ("DC1 equals DC2?")
+ dc1.equals (DC2));
SYSTEM.OUT.PRINTLN ("DC1 ' s hashcode:" + dc1.hashcode ());
System.out.println ("DC2 ' s hashcode:" + dc2.hashcode ());

System.out.println ("DC1:" + dc1.tostring ());
System.out.println ("DC2:" + dc2.tostring ());

/* Make modifications to the cloned array * *
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; J < Arr2[i].length; J + +) {
Dc2.getarr () [i][j] + + 10;
}
}

/* Print the clone and then make the modified array * *
System.out.println ("Modify the arr2 after DC2");
System.out.println ("ARR2:");
For (int[] AR:DC2.GETARR ()) {
for (int aa:ar) {
System.out.print (AA + ",");
}
System.out.println ();
}

System.out.println ("DC1 equals DC2?")
+ dc1.equals (DC2));
SYSTEM.OUT.PRINTLN ("DC1 ' s hashcode:" + dc1.hashcode ());
System.out.println ("DC2 ' s hashcode:" + dc2.hashcode ());

System.out.println ("DC1:" + dc1.tostring ());
System.out.println ("DC2:" + dc2.tostring ());

catch (Clonenotsupportedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
/* Two objects with different arrays to compare * *
System.out.println ("DC1 equals DC3?" + dc1.equals (DC3));

 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.