Java-copyOf

copyOf

The copyOf() method returns a copy of an array and has the following forms :

The original array is specified by source, and the length of the copy is specified by len. If the copy is longer than source, then the copy is padded with zeros, nulls or false. If the copy is shorter than source, then the copy is truncated.

Program

Program Source

import java.util.Arrays;

public class Javaapp {
 
    public static void main(String[] args) {
     
        int[] array_v1 = {10,20,30,40,50};
        int[] array_v2 = Arrays.copyOf(array_v1,3);
     
        for(int i=0;i<array_v2.length;i++)
        {
            System.out.println("array_v2["+i+"] = "+array_v2[i]);
        }
    }
}

Leave a Comment