+ Reply to Thread
Results 1 to 4 of 4

Thread: Copying Arrays

  1. #1
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,042

    Copying Arrays

    Copying Arrays

    You might have an array that you need a copy of. This might occur if you need to do some sorting but do not want to destroy the original array.

    Java provides four built in methods for making a copy of an array.

    These methods are:
    • Object.clone()
    • Arrays.copyOf
    • Arrays.copyOfRange
    • System.arraycopy

    The Arrays class is located in the java.util package.

    Clone method

    I'm going to start with clone because it is a bit interesting to note the theory behind it. This method we are going to use is defined in the Object class. You may ask, so what? What does this have to do with arrays? To answer this, let us take a look at declaring an object. How do you declare an object? To create an object we use the new operator.

    Example:

    Code:
    Integer i = new Integer(5);
    We use the new operator to allocate a new Object in memory. Now let us look at how we declare an array.

    Code:
    int[] nums = new int[3];
    Notice, how we also use the new operator? So if we are using the new operator then the array must be an object. You can use various properties of the array such as length by typing .length after the array name. So this means that the array must be an object. A little side note for users of Netbeans. In Netbeans, you can press Ctrl-B on an objects name to open the class file for that object. I tried this with an array and I got nothing. So how is the array created? If you look at the Object class it is declared much like an interface but it's methods are declared to be native meaning they are not written in Java but some form of C or C++. This has nothing to do with cloning objects but is a bit interesting.

    Example of cloning an array:

    Code:
    int[] nums = {3,2,4,5,6};
    int[] a = nums.clone();
    You can verify that this works by outputting the array. You do not get a reference to the array, and modifying one modifies the other. You get two exact copies of the array. This just as easily works with with multidimensional arrays.

    Code:
    int[][] nums =  {
    	{3,2,4,5},
    	{2,4,6,7},
    	{3,4,5,6}
    };
    
    
    int[][] b = nums.clone();
    Arrays.copyOf

    This method is more flexible than the clone method. This method copies the array but you can specify the length of the new array.

    If the new length is greater than the previous length then the end of the array is padded with default values. If the new length is less than the length of the array being copied then length characters are copied.

    Consider this:

    Code:
    int[] nums = {3,2,5,6,9,10};
    int[] c = Arrays.copyOf(nums,nums.length); // This makes an exact copy of the array. This is the same as nums.clone()
    int[] b = Arrays.copyOf(nums,3); // copies the first 3 items. The resulting array is {3,2,5}
    int[] a = Arrays.copyOf(nums,10); // copies the entire array and the remaining portion of the array is set to 0.  The resulting array is {3,2,5,6,9,10,0,0,0,0}

    Arrays.copyOfRange


    This has to be the most flexible function for copying arrays. You specify the indices of the array to be copied.

    The method prototype is as follows:

    Code:
    T[] Arrays.copyOfRange(T[] arr, int start, int end)
    This means that it is a generic function. The method copies all the values in the range [start,end) into a new array and returns the new array.

    Example:

    Code:
    int[] nums = {3,2,4,5,6,7,8,9,10,11};
    int[] copy = Arrays.copyOfRange(nums,2,6); 
    
    for (int i=0;i<copy.length;i++) {
    	System.out.print(copy[i] + " ");
    }
    System.out.println();
    This code creates a new array that consists of the elements in nums from index 2 to index 6 (but not including the item at index 6.)

    System.arraycopy

    This is another method that makes copies of arrays. However, I am not going to cover what it does. As I simply don't care. The way I see it, Arrays.copyOf and Arrays.copyOfRange do everything that we would ever need. So I don't see that System.arraycopy could possibly do anything that the two methods in java.util.Arrays could not do.


    Manual Copying


    Another method for copying arrays is manual copying. Basically you use loops and manually assign items to a new array. Using this method to copy an array of N dimensions requires N nested loops. I'm sure you could use Arrays.copyOf and Arrays.copyOfRange with multidimensional arrays as well. Granted I haven't figured that out yet but I'm sure it is possible.

    Enjoy!
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    13,155
    Blog Entries
    59

    Re: Copying Arrays

    I love things like this because it shows some of the subtle differences between "the Java way" and "the C++ way" of doing things. +rep
    CodeCall Blog | CodeCall Wiki
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,750
    Blog Entries
    97

    Re: Copying Arrays

    Very cool! +rep

  4. #4
    Programming Professional Sinipull will become famous soon enough Sinipull's Avatar
    Join Date
    Jun 2009
    Location
    Estonia
    Age
    22
    Posts
    254

    Re: Copying Arrays

    Great tutorial, i didn't know Java had built in methods for that. Always used loops.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Multi-dimensional arrays
    By WingedPanther in forum C Tutorials
    Replies: 5
    Last Post: 11-15-2009, 06:40 PM
  2. Multidimensional Arrays
    By chili5 in forum Java Tutorials
    Replies: 22
    Last Post: 08-14-2009, 08:02 AM
  3. Char arrays in C
    By outsid3r in forum C Tutorials
    Replies: 4
    Last Post: 07-28-2009, 12:38 PM
  4. PHP Arrays
    By chili5 in forum PHP Tutorials
    Replies: 7
    Last Post: 05-22-2009, 01:16 PM
  5. Replies: 0
    Last Post: 05-07-2008, 04:36 AM