Arrays copyOf() in Java with examples
java.util.Arrays.copyOf() method is in java.util.Arrays class. It copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length.
copyOf(int original, int newLength)
- original – original array
- newLength – copy of original array
Original Array 1 2 3 New array copy after modifications: 1 2 3 11 55
What happens if length of copied array is greater than original array?
The two arrays will have same values for all the indices that are valid in original array and new array.
However, the indices missing in original will have zero in copy in case the copied array length is more than the original array.
Original Array : 1 2 3 New array copy (of higher length): 1 2 3 0 0
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.