Set One Array Equal to Another in Java

Sheeraz Gul Apr 07, 2022
Set One Array Equal to Another in Java

There are several to make an array equal to another in Java. This tutorial demonstrates how to set one array value equal to another in Java.

Set One Array Equal to Another in Java

As mentioned above, arrays in Java can be set equal to another array using several ways. Here are a few ways:

  1. Create an array with the same length as the previous and copy every element.
  2. Using the System.ArrayCopy() method, we can copy a subset of an array. It takes two arrays and the length of the output array as parameters.
  3. We can clone an array using the clone() method.
  4. Using Array.copyof() and Arrays.copyOfRange() methods we can also copy an array. The Arrays.copyof() method takes array and number of elements as parameters and Arrays.copyOfRange() takes array and range of elements as parameters.
  5. Also, one most simple way to assign one array to another, but it has a side effect that if we change an element of one array, it will be later changed in the second array. All the methods above don’t have that side effect.

Let’s try an example where we can implement all the methods above.

package delftstack;

import java.util.Arrays;

public class Array_Copy {
    public static void main(String args[]) {

        //Method 1: Copy array by the same length
        String[] Array1 = {"delftstack1", "delftstack2", "delftstack3"};
        String[] Array2 = new String[Array1.length];
        for (int i = 0; i < Array2.length; i++) {
           Array2[i] = Array1[i];
        System.out.println("Method 1: ");
        System.out.print("Array 1: "+Arrays.toString(Array1));
        System.out.print("\nArray 2: "+Arrays.toString(Array2));

        //Method 2: Using System.arraycopy method
        String[] Array3 = {"delftstack1", "delftstack2", "delftstack3"};
        String[] Array4 = new String[Array3.length];
        System.arraycopy(Array3, 0, Array4, 0, Array4.length);

        System.out.println("\nMethod 2: ");
        System.out.print("Array 3: "+Arrays.toString(Array3));
        System.out.print("\nArray 4: "+Arrays.toString(Array4));

        //Method 3: using clone() method
        String[] Array5 = {"delftstack1", "delftstack2", "delftstack3"};
        String[] Array6 = Array5.clone();

        System.out.println("\nMethod 3: ");
        System.out.print("Array 5: "+Arrays.toString(Array5));
        System.out.print("\nArray 6: "+Arrays.toString(Array6));

        //Method 4: using Arrays.copyOf() and Arrays.copyOfRange() methods
        String[] Array7 = {"delftstack1", "delftstack2", "delftstack3"};
        String[] Array8 = Arrays.copyOf(Array7, 3);
        String[] Array9 = Arrays.copyOfRange(Array7, 0, 3);

        System.out.println("\nMethod 4: ");
        System.out.print("Array 7: "+Arrays.toString(Array7));
        System.out.print("\nArray 8: "+Arrays.toString(Array8));
        System.out.print("\nArray 9: "+Arrays.toString(Array9));

        //Method 5: using clone() method
        String[] Array10 = {"delftstack1", "delftstack2", "delftstack3"};
        String[] Array11 = Array10;

        //Check the side effect
        Array10[2]= "Changed";
        System.out.println("\nMethod 5: ");
        System.out.print("Array 10: "+Arrays.toString(Array10));
        System.out.print("\nArray 11: "+Arrays.toString(Array11));



The code above uses all the methods described above and sets one array equal to another in Java, copying an array. See output:

Method 1:
Array 1: [delftstack1, delftstack2, delftstack3]
Array 2: [delftstack1, delftstack2, delftstack3]
Method 2:
Array 3: [delftstack1, delftstack2, delftstack3]
Array 4: [delftstack1, delftstack2, delftstack3]
Method 3:
Array 5: [delftstack1, delftstack2, delftstack3]
Array 6: [delftstack1, delftstack2, delftstack3]
Method 4:
Array 7: [delftstack1, delftstack2, delftstack3]
Array 8: [delftstack1, delftstack2, delftstack3]
Array 9: [delftstack1, delftstack2, delftstack3]
Method 5:
Array 10: [delftstack1, delftstack2, Changed]
Array 11: [delftstack1, delftstack2, Changed]
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Array