Java Array class copyOf(T[] original, int newLength) example
Array class copyOf(T[] original, int newLength) method example
Array class copyOf(T[] original, int newLength)method example. This example shows you how to use copyOf(T[] original, int newLength)method.This method Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.
Here is the code:-
/**
* @Program that Copies the specified array, truncating or padding with nulls
* (if necessary) so the copy has the specified length.
* Copy8Array.java
* Author:-RoseIndia Team
* Date:-15-May-2008
*/
import java.util.*;
public class Copy8Array {
public static void main(String[] args) {
Object arr[]={"Rose","India","Limited","Rohini"};
//Copies the specified object array,
Object a[]=Arrays.copyOf(arr,5);
for(int i=0;i<a.length;i++)
System.out.println("The Copied Array:-"+a[i]);
}
} |
Output of the program:-
The Copied Array:-Rose
The Copied Array:-India
The Copied Array:-Limited
The Copied Array:-Rohini
The Copied Array:-null |
|