Jump to content




Recent Status Updates

View All Updates

Developed by TechBiz Xccelerator
Photo
- - - - -

Copying Arrays

nested loop array

  • Please log in to reply
3 replies to this topic

#1 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3,033 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 25 July 2009 - 04:54 AM

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:

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.

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:

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.

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:

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:

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:

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!
  • 2

#2 WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderator
  • 17,278 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 25 July 2009 - 05:02 AM

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
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 25 July 2009 - 05:15 AM

Very cool! +rep
  • 0

#4 Sinipull

Sinipull

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 384 posts

Posted 26 July 2009 - 02:28 AM

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





Also tagged with one or more of these keywords: nested loop, array

Powered by binpress