Copy of the Java array Four methods: For, clone, System.arraycopy, arrays.copyof

Source: Internet
Author: User

 Public classarraycopy{ Public Static voidMain (String []args) {int[]a = {1,3,4,5};    Toprint (a); int[]afor=New int[A.length]; //1.for Cyclic replicationSystem.out.println ("===========1. Using for Replication");  for(inti=0;i<a.length;i++) {Afor[i]=A[i]; } afor[2]=10;//Change the value in the original array of values in the Afor unchangedSystem.out.print ("Array A:");    Toprint (a); System.out.print ("Array afor:");    Toprint (afor); //**2. Using System.arraycopy (src,srcpos,dest,destpos,length);System.out.println ("===========2. Use system.arraycopy copy \ n Copy afor to a:"); System.arraycopy (Afor,0,a,0, a.length); afor[1]=9;//change the value of AforToprint (a);    Toprint (afor); //3. Copy using CloneSystem.out.println ("===========3. Using clone to copy the value of Afor to a"); A=(int[]) Afor.clone (); afor[0]=8;    Toprint (a);    Toprint (afor); //4. Using copyof and Copyofrange of the arrays class for array replicationSystem.out.println ("===========4. Use arrays.copyof/to copy the value of Afor to a"); A=java.util.arrays.copyof (afor,afor.length+1); afor[3]=11;    Toprint (a);        Toprint (afor); }    Static voidToprint (int[] a) {     for(intaa:a) {System.out.print (" "+AA);    } System.out.println (); }}

Copy of the Java array Four methods: For, clone, System.arraycopy, arrays.copyof

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.