Copying Arrays

3 November 2007

Arrays are very useful and are commonly used in our Java programs. Array name is actually a pointer that points to the continuous memory location. Many Java developers make some mistakes while copying arrays, which become difficult to cater.

Consider the following scenario:

I have a String array called ‘array’ which have 5 elements in it. I declared another String array called ‘array_tmp’. I want to copy the contents of ‘array’ to ‘array_tmp’. One simple way is to do that in a for loop, but its hectic. Usually Java programmers/developers try to do the following:

String array[] = new String[5];
String array_tmp[] = new String[5];
 
for(int i=0;i<array.length;i++)
{
array[i] = "String" + i;
}
array_tmp = array;
array[1] = "CHANGED";
 
for(int i=0;i<array.length;i++)
{
System.out.println("Array - Value at index " + i + " is " + array[i]);
}
 
for(int i=0;i<array_tmp.length;i++)
{
System.out.println("Array_tmp - Value at index " + i + " is " + array_tmp[i]);
}

Output:

Array - Value at index 0 is String0
Array - Value at index 1 is CHANGED
Array - Value at index 2 is String2
Array - Value at index 3 is String3
Array - Value at index 4 is String4
Array_tmp - Value at index 0 is String0
Array_tmp - Value at index 1 is CHANGED
Array_tmp - Value at index 2 is String2
Array_tmp - Value at index 3 is String3
Array_tmp - Value at index 4 is String4

Here we tried to copy arrays using equal operator. After that we changed the value at index 1 of ‘array’.While printing, we noticed that value at index 1 of ‘array_tmp’ also got changed. This is because the ‘array’ was not copied to ‘array_tmp’. The reference was copied so ‘array’ is also referred as ‘array_tmp’ and both are refering to the same array.

Correct way is to use clone method to copy array. Clone method copied the contents of one array to other array. One can also use a for loop to copy contents on one array to other but as already said, it is a bit hectic. Another acceptable way is to use arrayCopy methood where you can specify which part of the array to copy.

String array[] = new String[5];
String array_tmp[] = new String[5];
 
for(int i=0;i<array.length;i++)
{
array[i] = "String" + i;
}
array_tmp = array;
 
array_tmp = array.clone();
array[1] = "CHANGED";
 
for(int i=0;i<array.length;i++)
{
System.out.println("Array - Value at index " + i + " is " + array[i]);
}
 
for(int i=0;i<array_tmp.length;i++)
{
System.out.println("Array_tmp - Value at index " + i + " is " + array_tmp[i]);
}

Output:

Array - Value at index 0 is String0
Array - Value at index 1 is CHANGED
Array - Value at index 2 is String2
Array - Value at index 3 is String3
Array - Value at index 4 is String4
Array_tmp - Value at index 0 is String0
Array_tmp - Value at index 1 is String1
Array_tmp - Value at index 2 is String2
Array_tmp - Value at index 3 is String3
Array_tmp - Value at index 4 is String4

del.icio.us:Copying Arrays  digg:Copying Arrays  spurl:Copying Arrays  wists:Copying Arrays  simpy:Copying Arrays  newsvine:Copying Arrays  blinklist:Copying Arrays  furl:Copying Arrays  reddit:Copying Arrays  fark:Copying Arrays  blogmarks:Copying Arrays  Y!:Copying Arrays  smarking:Copying Arrays  magnolia:Copying Arrays  segnalo:Copying Arrays  gifttagging:Copying Arrays

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Copying Arrays

Leave a Reply