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:
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:
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 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:
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:
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 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:
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:
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 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:
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:
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 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:
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:
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 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:
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:
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 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:
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:
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 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:
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:
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 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:
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 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:
“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:
The output is:
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>.