6. Arrays

 
[Page 177 ( continued )]

6.3. Copying Arrays

Often, in a program, you need to duplicate an array or a part of an array. In such cases you could attempt to use the assignment statement ( = ), as follows :

 list2 = list1; 

This statement does not copy the contents of the array referenced by list1 to list2 , but merely copies the reference value from list1 to list2 . After this statement, list1 and list2 reference to the same array, as shown in Figure 6.4. The array previously referenced by list2 is no longer referenced; it becomes garbage, which will be automatically collected by the Java Virtual Machine.

Figure 6.4. Before the assignment statement, list1 and list2 point to separate memory locations. After the assignment, the reference of the list1 array is passed to list2 .
(This item is displayed on page 178 in the print version)


In Java, you can use assignment statements to copy primitive data type variables, but not arrays. Assigning one array variable to another array variable actually copies one reference to another and makes both variables point to the same memory location.


[Page 178]

There are three ways to copy arrays:

  • Use a loop to copy individual elements one by one.

  • Use the static arraycopy method in the System class.

  • Use the clone method to copy arrays; this will be introduced in Chapter 9, "Inheritance and Polymorphism."

You can write a loop to copy every element from the source array to the corresponding element in the target array. The following code, for instance, copies sourceArray to targetArray using a for loop:

   int   [] sourceArray = {   2   ,   3   ,   1   ,   5   ,   10   };   int   [] targetArray =   new int   [sourceArray.length];   for   (   int   i =     ; i < sourceArray.length; i++) {   targetArray[i] = sourceArray[i]; } 

Another approach is to use the arraycopy method in the java.lang.System class to copy arrays instead of using a loop. The syntax for arraycopy is shown below:

 arraycopy(sourceArray, srcPos, targetArray, tarPos, length); 

The parameters srcPos and tarPos indicate the starting positions in sourceArray and targetArray , respectively. The number of elements copied from sourceArray to targetArray is indicated by length . For example, you can rewrite the loop using the following statement:

 System.arraycopy(sourceArray,     , targetArray,     , sourceArray.length); 

The arraycopy method does not allocate memory space for the target array. The target array must have already been created with its memory space allocated. After the copying takes place, targetArray and sourceArray have the same content but independent memory locations.

Note

The arraycopy method violates the Java naming convention. By convention, this method should be named arrayCopy (i.e., with an uppercase C ).


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net