Go Back   CodeCall Programming Forum > Software Development > Tutorials > Java Tutorials
Register Blogs Search Today's Posts Mark Forums Read

Java Tutorials Tutorials and Code for Java

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-25-2009, 08:54 AM
chili5's Avatar
Code Slinger
 
Join Date: Mar 2008
Posts: 7,018
chili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond repute
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-25-2009, 09:02 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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 | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-25-2009, 09:15 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: Copying Arrays

Very cool! +rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-26-2009, 06:28 AM
Sinipull's Avatar
Programmer
 
Join Date: Jun 2009
Location: Estonia
Age: 21
Posts: 151
Sinipull will become famous soon enough
Re: Copying Arrays

Great tutorial, i didn't know Java had built in methods for that. Always used loops.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Multi-dimensional arrays WingedPanther C Tutorials 5 11-15-2009 09:40 PM
Java Multidimensional Arrays chili5 Java Tutorials 22 08-14-2009 12:02 PM
Char arrays in C outsid3r C Tutorials 4 07-28-2009 04:38 PM
PHP Arrays chili5 PHP Tutorials 7 05-22-2009 05:16 PM
What arrays and pointers have in common - or - Why arrays start at zero Kernel News 0 05-07-2008 08:36 AM


All times are GMT -5. The time now is 10:30 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0