Copy full solution of Java array

Source: Internet
Author: User

1. Assigning a reference to an array of basic data types to another array
1  Public classarray_copy {2     int[] array1=New int[]{1,2,3,4,5,6};3     int[] array2=array1;//assigns a reference to the array1 to Array2, and the two arrays point to the same memory space4      Public Static voidMain (String args[]) {5array_copy AC =Newarray_copy ();6          for(inti=0;i<ac.array1.length;i++){7System.out.print (ac.array1[i]+ "");8         }9 System.out.println ();Ten          for(inti=0;i<ac.array1.length;i++){ OneSystem.out.print (ac.array2[i]+ ""); A         } -System.out.println ("\ n" + "modifies the value of the array array1 to see if the array2 has changed"); -ac.array1[0]=10;//modifying the first element of an array the          for(inti=0;i<ac.array1.length;i++){ -System.out.print (ac.array1[i]+ ""); -         } - System.out.println (); +          for(inti=0;i<ac.array1.length;i++){ -System.out.print (ac.array2[i]+ ""); +         } A     } at}

This is just a copy of the array reference, and an array change affects the other array. 2. Use the Arraycopy method to copy the base data type array 2.1. Arraycopy Method declaration
 Public Static native void arraycopy (Object src,int srcpos,object dest,int destpos,int lenhgth);
2.2. Introduction to Arraycopy Method parameters

SRC: Source Array

< Span class= "token li" > Srcpos: Where to start copying, starting from where in the source array

< Span class= "token li" >< Span class= "token MD md-strong" >dest: Destination array

< Span class= "token li" >< Span class= "token MD md-strong" >< Span class= "token li" > Descpos: Copy the source array to the starting position of the destination array

< Span class= "token li" >< Span class= "token MD md-strong" >< Span class= "token li" >< Span style= "Font-family:arial, Helvetica, Sans-serif;" >length: How many elements in a source array are copied

< Span class= "token li" >< Span class= "token MD md-strong" >< Span class= "token li" >< Span class= "token MD md-strong" > 2.2. Arraycopy Method Demo
1  Public classArraycopy {2     int[] Ary1 =New int[]{1, 2, 3, 4, 5, 6};3     int[] Ary2 =New int[6];4      Public  voidPrintint[] Array) {5          for(intI:array)6System.out.print (i+ "");7 System.out.println ();8     }9     /**Ten * Use the Arraycopy method to copy the array ary1 to Ary2 One      * @paramargs A      */ -      Public Static voidMain (string[] args) { -Arraycopy AC =NewArraycopy (); theSYSTEM.OUT.PRINTLN ("---Two array initial---"); - Ac.print (ac.ary1); - Ac.print (ac.ary2); -SYSTEM.OUT.PRINTLN ("---copy ary1 to Ary2---"); +System.arraycopy (ac.ary1,0,ac.ary2,0, ac.ary1.length); - Ac.print (ac.ary1); + Ac.print (ac.ary2); ASYSTEM.OUT.PRINTLN ("---modify Ary1 (ary2) to see if Ary2 (ary1) Changes---"); atAc.ary1[0]=0; - Ac.print (ac.ary1); - Ac.print (ac.ary2); -     } -}

Using the Arraycopy method for array copying, there is no reference to the array, that is: the contents of an array are changed without affecting the contents of the other array.

3. Copying of an array of object types (reference types)
1 Importjava.awt.*;2 3  Public classArraycopy2 {4     /**5 * Define an array of objects6      */7Label lb1[] =Newlabel[]{8             NewLabel ("Label1"),9             NewLabel ("Label2"),Ten             NewLabel ("Label3") One     }; ALabel lb2[] =NewLabel[lb1.length]; -  -      Public Static voidMain (String args[]) { theArraycopy2 AC =NewArraycopy2 (); -System.out.println ("--Copy Object array lb1 to lb2--"); -System.out.print ("LB1 array:"); - Ac.print (AC.LB1); +         //ac.lb2 = AC.LB1; -System.arraycopy (ac.lb1,0,ac.lb2,0, ac.lb1.length); +System.out.print ("LB2 array:"); A Ac.print (AC.LB2); atSYSTEM.OUT.PRINTLN ("\n--modify LB1 (LB2) to see if LB2 (LB1) Changes--"); -Ac.lb1[0].settext ("Label0"); -System.out.print ("LB1 array:"); - Ac.print (AC.LB1); -System.out.print ("LB2 array:"); -Ac.print (AC.LB2);//after modifying the contents of the LB1 array, it was found that the LB2 array also changed, indicating that LB1 and Lb2 point to the same memory space.  in         //of course, modify LB2 content LB1 content will also change, here do not repeat -     } to  +      Public voidprint (label[] lb) { -          for(Label i:lb) { theSystem.out.print (I.gettext () + ""); *         } $ System.out.println ();Panax Notoginseng     } -}
And the primitive type array is different, when the array type is an object type, copying the array with the Arraycopy method is only a reference, not the object itself.

Related blog (CSDN): 80262416

Copy full solution of Java array

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.