Copy an array to a different runtime type in Java
In this post, we’ll discuss how to copy an array to a different runtime type in Java.
There are several ways to copy an array to a different runtime type in Java, which is the process of transferring the elements of one array to another array of compatible data type. Some of the possible methods are:
1. Using Casting
We know that arrays are objects in Java, but they are a container object that holds elements of a single type. Therfore, to cast the source array T[] to the destination array U[], each member of the source array must be casted to type U. The idea is to use a loop to iterate over the elements of the source array and cast each element to the destination type. This method requires us to know the types of both arrays and handle any possible exceptions that may occur due to incompatible types. For example, the following code uses a for loop to copy and cast each element of a Number array to an Integer array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.Arrays; class Main { /** * Copies the specified array * * @param <T> The class of the objects in the source array * @param <U> The class of the objects in the destination array * @param source The source array * @param dest The destination array * * @throws NullPointerException if original is null * @throws ArrayStoreException if an element copied from source is not of * a runtime type that can be stored in an array of class U */ public static <T, U> void copyArray(T[] source, U[] dest) { for (int i = 0; i < source.length; i++) { dest[i] = (U) source[i]; } } public static void main(String[] args) { Number[] source = { 1, 2, 3, 4, 5 }; Integer[] dest = new Integer[source.length]; try { copyArray(source, dest); } catch (ArrayStoreException e) { e.printStackTrace(); } System.out.println(Arrays.toString(dest)); // [1, 2, 3, 4, 5] } } |
2. Using Arrays.copyOf() method
Another option is to use the Arrays.copyOf() method from the java.util package, which is used to copy an array into a new array of a specified type and length. It has an overloaded version where we can specify the type of the resulting array. This method can handle the casting of the elements internally and throw an ArrayStoreException if an element cannot be stored in the destination array. For example, the following code uses the Arrays.copyOf() method to copy each element of Number[] to Integer[]:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.Arrays; class Main { /** * Copies the specified array * * @param <T> The class of the objects in the original array * @param <U> The class of the objects in the returned array * @param source The array to be copied * @param newType The class of the copy to be returned * * @return a copy of the original array * * @throws NullPointerException if original is null * @throws ArrayStoreException if an element copied from original is not of * a runtime type that can be stored in an array of class newType */ public static <T, U> U[] copyArray(T[] source, Class<U[]> newType) { return Arrays.copyOf(source, source.length, newType); } public static void main(String[] args) { Number[] source = { 1, 2, 3, 4, 5 }; try { Integer[] dest = copyArray(source, Integer[].class); System.out.println(Arrays.toString(dest)); // [1, 2, 3, 4, 5] } catch (ArrayStoreException e) { e.printStackTrace(); } } } |
Similar to the Arrays.copyOf() method, the Arrays class has another method called copyOfRange(), which is overloaded to accept the type of the resulting array. We can also use the System.arraycopy() method from the java.lang package, which can copy a specified portion of an array into another array.
That’s all about copying an array to a different runtime type in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)