Best Way To Copy Array In Java

Hey!! How are you doing!!? In this post I’m going to show you the best way to copy array in java.

We are going to learn how to copy the contents of one array to another array.

independent memory location

For example let’s say in our program

public class ArrayCopyExample
{
public static void main(String[] args)
{
int count1, count2;
count1 = 500;
count2 = count1;
System.out.println("The value of count1 - " + count1 + "\nThe value of count2 - " + count2);
}
}

I have two integer variables count1 and count2. I am initializing count1 with a value of 500.

Now if you want to copy the value of variable count1 to variable count2 then what you can do is you can write

count2 = count1;

So now both count1 and count2 are going to contain the value 500 and they are going to be in the different memory locations.

If you run the above program

Best Way To Copy Array In Java

You can see count1 is 500 and count2 is 500. Now we will change the value of count1 variable after copying the value of count1 to count2.

Let’s say 300, that is,

public class ArrayCopyExample
{
public static void main(String[] args)
{
int count1, count2;
count1 = 500;
count2 = count1;
count1 = 300;
System.out.println("The value of count1 - " + count1 + "\nThe value of count2 - " + count2);
}
}

If I run this program you will see this output,

Output:

The value of count1 - 300
The value of count2 - 500

As you can see the output, count1 is 300 and count2 is 500 and it means that count1 and count2 are stored independently of each other.

So after copying two variables you know count1 will be stored in some memory location and count2 will be stored in some other memory location.

They are independent of each other.


Best Way To Copy Array In Java

Arrays point to same memory location

But when we use arrays in our programs you know we cannot use assignment ( = ) operator to copy the contents of an array to another array.

Here I will demonstrate,

public class ArrayCopyExample
{
public static void main(String[] args)
{
int numbers[] = {10,20,30,40,50};
int copyNumber[] = new int[5];
copyNumber = numbers;
}
}

I have an array of type integer and let’s say it is going to be “numbers” and I have initialized (as you can see above).

Then we are going to create another array and let’s call it as “copyNumber” and have allocated the memory so that it can store five values.

That’s because in “numbers” array I have five elements. All right we have two arrays one is “numbers” another one is “copyNumber”.

Now if you want to copy the contents of “numbers” to “copyNumber” then we write,

copyNumber = numbers;

What happens here is this “numbers” and “copyNumber” are going to point to the same memory location.

Let me explain,

Best Way To Copy Array In Java

When you create “numbers” array what happens is “numbers” array will be created and it’s going to store the five elements.

And then when you create “copyNumber” and then we allocate memory locations to store five values, at that time the memory locations will be allocated so that it can store the five values.

We have used ( = ) assignment operator and we have assigned “numbers” to “copyNumber”. So here “numbers” is the name of the variable which is going to contain the address of the first element of the array.

This variable name “numbers” is going to contain the address of first element of “numbers” array. So let’s say, for example, it has address 14656.

Then “numbers” is going to contain the address of first element of “numbers” array, that is, 10.

Here variable name “copyNumber” also contain the address of first element of “copyNumber” array. Now it is containing the default value which is going to be zero. (As you can see in the above figure)

You know when you allocate the memory for the arrays they are going to contain the default value.

Then when we write

copyNumber = numbers;

Using assignment operator at that time this “copyNumber” is gonna contain the value which is stored in “numbers” which is nothing but the address of first element of “numbers” array.

So now first element of “copyNumber” array memory will not be referenced and it will be deleted by java automatically.

We can say that “numbers” and “copyNumber” are pointing to the same memory location. So if I change the value of “numbers” array then that will be reflected in “numbers” and “copyNumber” array.

Just to demonstrate that what I’m going to do is print the values of “copyNumber” and “number”.

public class ArrayCopyExample
{
public static void main(String[] args)
{
int numbers[] = {10,20,30,40,50};
int copyNumber[] = new int[5];
copyNumber = numbers;
for(int a = 0;a < numbers.length;a++)
{
System.out.println(numbers[a]);
}
for(int a = 0;a < copyNumber.length;a++)
{
System.out.println(copyNumber[a]);
}
}
}

Output:

10
20
30
40
50
10
20
30
40
50

You guys can see 10,20,30,40,50 then again it is repeated. That’s because both arrays are pointing to the same values stored in the memory.

If I change the value of “numbers” array, that is third element to

numbers[2] = 70;

And now if you run this program you get this output,

10
20
70
40
50
10
20
70
40
50

You can see the changed value is reflected in both the arrays.


we want arrays to be stored in different memory location

We don’t want this. When we copy the arrays you know we want them to be stored in different memory location and also they should be independent of each other.

In java when you need to copy the elements of one array to another, you have three options available.

The first one is, you can use a for loop and copy the elements one by one. Or you can use a static method which is available from the system class which is called arraycopy.

 Follow this link for arraycopy explanation and example.

Now let’s see how to do using a for loop,

public class ArrayCopyExample
{
public static void main(String[] args)
{
int numbers[] = {10,20,30,40,50};
int copyNumber[] = new int[5];
for(int a = 0;a < numbers.length;a++)
{
copyNumber[a] = numbers[a];
}
for(int a = 0;a < numbers.length;a++)
{
System.out.println(numbers[a]);
}
for(int a = 0;a < copyNumber.length;a++)
{
System.out.println(copyNumber[a]);
}
}
}

You know the below statement,

copyNumber[a] = numbers[a];

Is going to copy element by element and it’s going to paste that in copyNumber.

If I run the above program,

10
20
30
40
50
10
20
30
40
50

After this if I change the value of the “numbers” array, that is, let’s say 100

public class ArrayCopyExample
{
public static void main(String[] args)
{
int numbers[] = {10,20,30,40,50};
int copyNumber[] = new int[5];
for(int a = 0;a < numbers.length;a++)
{
copyNumber[a] = numbers[a];
}
numbers[2] = 100;
for(int a = 0;a < numbers.length;a++)
{
System.out.println(numbers[a]);
}
for(int a = 0;a < copyNumber.length;a++)
{
System.out.println(copyNumber[a]);
}
}
}

And if I run this program

10
20
100
40
50
10
20
30
40
50

As you can see above output “number” array has changed and “copyNumber” array has not changed.

So both the arrays are stored in different memory locations. They are not going to point to the same memory location.

Here we are copying element by element.

conclusion:

So this is it guys. This is how you guys can copy one array or you know copy the elements of one array to another array. I hope you have understood the best way to copy array in java.

If you found this post useful share it across social networks or you can subscribe by email to my blog flower brackets.

About the author

admin

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *