Java

How to copy array in Java

If one array is copied to another, the result should be that there are two independent arrays with two different names but with the same corresponding values. A change in the value of one array element should not result in any change in the corresponding element of the other array.

When an array is truly copied to another, if the length of the new array is shorter than the length of the original array, then the new array is a copy but truncated at the copied length. If the new array is longer, then the extra elements towards its end are filled with the default values of the data type. For one array to be copied to another, both arrays must be of the same type or compatible types.

Java has a method to copy one array to another. This method is the copyOf() method, and it is overloaded for the different data types. It is a static method of the Array class. “static” means an array does not have to be instantiated for the method to be used. It uses the class name, Array, with the first ‘A’ in uppercase. The method returns the copied array. This article explains the different overloaded forms of the copyOf() method. It includes how to copy reference types. How to copy an array range is not left out.

Copying an Array of Boolean Values

The syntax is:

static boolean[] copyOf(boolean[] original, int newLength)

original is the name of the original array. newLength is the length of the new or copied array. If it is shorter, the copy is truncated at newLength. If it is longer, false is padded as values to to the new array to have the new-length. The following program illustrates this:

    import java.util.Arrays;
    public class TheClass {
        public static void main(String[] args) {
boolean[] orig = {true, true, true, true, true};
boolean[] cpy1 = Arrays.copyOf(orig, 3);
            for (int i=0; i< cpy1.length; i++) {System.out.print(cpy1[i]); System.out.print(' ');} System.out.println();
boolean[] cpy2 = Arrays.copyOf(orig, 5);
            for (int i=0; i< cpy2.length; i++) {System.out.print(cpy2[i]); System.out.print(' ');} System.out.println();
boolean[] cpy3 = Arrays.copyOf(orig, 7);
            for (int i=0; i< cpy3.length; i++) {System.out.print(cpy3[i]); System.out.print(' ');} System.out.println();
        }
    }

The output is:

true true true

true true true true true

true true true true true false false

For the first output line, there is truncation. For the second output line, both arrays are the same. The new array is longer than the original array for the third output line.

The length of the new array is determined by the newLength parameter of the copyOf() method syntax.

Copying an Array of Byte Values

The syntax is:

static byte[] copyOf(byte[] original, int newLength)

original is the name of the original array. newLength is the length of the new or copied array. If it is shorter, the copy is truncated at newLength. If it is longer, 0 is padded as values to the new array, to have the new-length. The following main() method code illustrates this:

    static byte[] copyOf(byte[] original, int newLength)

original is the name of the original array. newLength is the length of the new or copied array. If it is shorter, the copy is truncated at newLength. If it is longer, 0 is padded as values to the new array, to have the new-length. The following main() method code illustrates this:

        public static void main(String[] args) {
byte[] orig = {1, 2, 3, 4, 5};
byte[] cpy1 = Arrays.copyOf(orig, 3);
            for (int i=0; i< cpy1.length; i++) {System.out.print(cpy1[i]); System.out.print(' ');} System.out.println();
byte[] cpy2 = Arrays.copyOf(orig, 5);
            for (int i=0; i< cpy2.length; i++) {System.out.print(cpy2[i]); System.out.print(' ');} System.out.println();
byte[] cpy3 = Arrays.copyOf(orig, 7);
            for (int i=0; i< cpy3.length; i++) {System.out.print(cpy3[i]); System.out.print(' ');} System.out.println();
        }

The output is:

1 2 3

1 2 3 4 5

1 2 3 4 5 0 0

The length of the new array is determined by the newLength parameter of the copyOf() method syntax.

Copying an Array of char Values

The syntax is:

static char[] copyOf(char[] original, int newLength)

original is the name of the original array. newLength is the length of the new or copied array. If it is shorter, the copy is truncated at newLength. If it is longer, ‘ ‘ is padded as values to the new array, to have the new-length. Code example:

        public static void main(String[] args) {
char[] orig = {'A', 'B', 'C', 'D', 'E'};
char[] cpy1 = Arrays.copyOf(orig, 3);
            for (int i=0; i< cpy1.length; i++) {System.out.print(cpy1[i]); System.out.print(' ');} System.out.println();
char[] cpy2 = Arrays.copyOf(orig, 5);
            for (int i=0; i< cpy2.length; i++) {System.out.print(cpy2[i]); System.out.print(' ');} System.out.println();
char[] cpy3 = Arrays.copyOf(orig, 7);
            for (int i=0; i< cpy3.length; i++) {System.out.print(cpy3[i]); System.out.print(' ');} System.out.println();
        }

The output is:

A B C

A B C D E

A B C D E ‘ ’ ‘ ’

The length of the new array is determined by the newLength parameter of the copyOf() method syntax.

Copying an Array of double Values

The syntax is:

static double[] copyOf(double[] original, int newLength)

original is the name of the original array. newLength is the length of the new or copied array. If it is shorter, the copy is truncated at newLength. If it is longer, 0.0 is padded as values to the new array, to have the new-length. Code example:

        public static void main(String[] args) {
double[] orig = {1.5, 2.5, 3.5, 4.5, 5.5};
double[] cpy1 = Arrays.copyOf(orig, 3);
            for (int i=0; i< cpy1.length; i++) {System.out.print(cpy1[i]); System.out.print(' ');} System.out.println();
double[] cpy2 = Arrays.copyOf(orig, 5);
            for (int i=0; i< cpy2.length; i++) {System.out.print(cpy2[i]); System.out.print(' ');} System.out.println();
double[] cpy3 = Arrays.copyOf(orig, 7);
            for (int i=0; i< cpy3.length; i++) {System.out.print(cpy3[i]); System.out.print(' ');} System.out.println();
        }

The output is:

1.5 2.5 3.5

1.5 2.5 3.5 4.5 5.5

1.5 2.5 3.5 4.5 5.5 0.0 0.0

The length of the new array is determined by the newLength parameter of the copyOf() method syntax.

Copying an Array of float Values

The syntax is:

static float[] copyOf(float[] original, int newLength)

original is the name of the original array. newLength is the length of the new or copied array. If it is shorter, the copy is truncated at newLength. If it is longer, 0.0 is padded as values to the new array, to have the new-length. Code example:

        public static void main(String[] args) {
float[] orig = {1.5f, 2.5f, 3.5f, 4.5f, 5.5f};
float[] cpy1 = Arrays.copyOf(orig, 3);
            for (int i=0; i< cpy1.length; i++) {System.out.print(cpy1[i]); System.out.print(' ');} System.out.println();
float[] cpy2 = Arrays.copyOf(orig, 5);
            for (int i=0; i< cpy2.length; i++) {System.out.print(cpy2[i]); System.out.print(' ');} System.out.println();
float[] cpy3 = Arrays.copyOf(orig, 7);
            for (int i=0; i< cpy3.length; i++) {System.out.print(cpy3[i]); System.out.print(' ');} System.out.println();
        }

The output is:

1.5 2.5 3.5

1.5 2.5 3.5 4.5 5.5

1.5 2.5 3.5 4.5 5.5 0.0 0.0

The length of the new array is determined by the newLength parameter of the copyOf() method syntax.

Copying an Array of int Values

The syntax is:

static int[] copyOf(int[] original, int newLength)

original is the name of the original array. newLength is the length of the new or copied array. If it is shorter, the copy is truncated at newLength. If it is longer, 0 is padded as values to the new array, to have the new-length. Code example:

        public static void main(String[] args) {
int[] orig = {1, 2, 3, 4, 5};
int[] cpy1 = Arrays.copyOf(orig, 3);
            for (int i=0; i< cpy1.length; i++) {System.out.print(cpy1[i]); System.out.print(' ');} System.out.println();
int[] cpy2 = Arrays.copyOf(orig, 5);
            for (int i=0; i< cpy2.length; i++) {System.out.print(cpy2[i]); System.out.print(' ');} System.out.println();
int[] cpy3 = Arrays.copyOf(orig, 7);
            for (int i=0; i< cpy3.length; i++) {System.out.print(cpy3[i]); System.out.print(' ');} System.out.println();
        }

The output is:

1 2 3

1 2 3 4 5

1 2 3 4 5 0 0

The length of the new array is determined by the newLength parameter of the copyOf() method syntax.

Copying an Array of Long Values

The syntax is:

static long[] copyOf(long[] original, int newLength)

original is the name of the original array. newLength is the length of the new or copied array. If it is shorter, the copy is truncated at newLength. If it is longer, 0 is padded as values to the new array, to have the new-length. Code example:

        public static void main(String[] args) {
long[] orig = {1, 2, 3, 4, 5};
long[] cpy1 = Arrays.copyOf(orig, 3);
            for (int i=0; i< cpy1.length; i++) {System.out.print(cpy1[i]); System.out.print(' ');} System.out.println();
long[] cpy2 = Arrays.copyOf(orig, 5);
            for (int i=0; i< cpy2.length; i++) {System.out.print(cpy2[i]); System.out.print(' ');} System.out.println();
long[] cpy3 = Arrays.copyOf(orig, 7);
            for (int i=0; i< cpy3.length; i++) {System.out.print(cpy3[i]); System.out.print(' ');} System.out.println();
        }

The output is:

1 2 3

1 2 3 4 5

1 2 3 4 5 0 0

The length of the new array is determined by the newLength parameter of the copyOf() method syntax.

Copying an Array of Short Values

The syntax is:

static short[] copyOf(short[] original, int newLength)

original is the name of the original array. newLength is the length of the new or copied array. If it is shorter, the copy is truncated at newLength. If it is longer, 0 is padded as values to the new array, to have the new-length. Code example:

        public static void main(String[] args) {
short[] orig = {1, 2, 3, 4, 5};
short[] cpy1 = Arrays.copyOf(orig, 3);
            for (int i=0; i< cpy1.length; i++) {System.out.print(cpy1[i]); System.out.print(' ');} System.out.println();
short[] cpy2 = Arrays.copyOf(orig, 5);
            for (int i=0; i< cpy2.length; i++) {System.out.print(cpy2[i]); System.out.print(' ');} System.out.println();
short[] cpy3 = Arrays.copyOf(orig, 7);
            for (int i=0; i< cpy3.length; i++) {System.out.print(cpy3[i]); System.out.print(' ');} System.out.println();
        }

The output is:

1 2 3

1 2 3 4 5

1 2 3 4 5 0 0

Copying an Array of Reference Data Types

A good example of the reference data type is the string object, instantiated from the string class. Its copyOf() syntax is the same as the above syntaxes. The following code illustrates this:

    import java.util.Arrays;
    public class TheClass {

        public static void main(String[] args) {
String[] orig = {"one", "two", "three", "four", "five"};
String[] cpy1 = Arrays.copyOf(orig, 3);
            for (int i=0; i< cpy1.length; i++) {System.out.print(cpy1[i]); System.out.print(' ');} System.out.println();
String[] cpy2 = Arrays.copyOf(orig, 5);
            for (int i=0; i< cpy2.length; i++) {System.out.print(cpy2[i]); System.out.print(' ');} System.out.println();
String[] cpy3 = Arrays.copyOf(orig, 7);
            for (int i=0; i< cpy3.length; i++) {System.out.print(cpy3[i]); System.out.print(' ');} System.out.println();
        }
    }

The output is:

one two three

one two three four five

one two three four five null null

The default value for the reference data type is null.

Copying a Range

The range of an array can be copied. The syntax to copy the range of an array of chars is:

static char[] copyOfRange(char[] original, int from, int to)

“from” is the first index, and “to” is the last index, whose value of the range, is just not included, in the copy. Example code:

        public static void main(String[] args) {
char[] orig = {'A', 'B', 'C', 'D', 'E'};
char[] cpy = Arrays.copyOfRange(orig, 1, 3);
            for (int i=0; i<cpy.length; i++) {System.out.print(cpy[i]); System.out.print(' ');} System.out.println();
        }

The output is:

B C

Copying the ranges of primitive and reference data types is similar to this code.

Conclusion

The Array class has the static overloaded method to copy arrays. It is used to copy the whole array. If the copied array is short, copying will be made to the truncated length indicated. If the copied array is longer than the original array, the default value will be padded for the extra added elements. The static copyOfRange() method can be used to copy a range. The next thing the reader should study is how to copy an array of generic types, <T>.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.