Skip to content
Related Articles

Related Articles

Improve Article
Arrays copyOf() in Java with examples
  • Difficulty Level : Easy
  • Last Updated : 07 Dec, 2018

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.

Syntax:

 copyOf(int[] original, int newLength) 
  • original – original array
  • newLength – copy of original array




// Java program to illustrate copyof method
import java.util.Arrays;
  
public class Main
{
    public static void main(String args[])
    {
        // initializing an array original
        int[] org = new int[] {1, 2 ,3};
  
        System.out.println("Original Array");
        for (int i = 0; i < org.length; i++)
            System.out.print(org[i] + " ");
  
        // copying array org to copy
        int[] copy = Arrays.copyOf(org, 5);
  
        // Changing some elements of copy
        copy[3] = 11;
        copy[4] = 55;
  
        System.out.println("\nNew array copy after modifications:");
        for (int i = 0; i < copy.length; i++)
            System.out.print(copy[i] + " ");
    }
}

Output:

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.




// Java program to illustrate copyOf when new array
// is of higher length.
import java.util.Arrays;
  
public class Main 
{
  public static void main(String args[]) 
  {
      // initializing an array original
      int[] org = new int[] {1, 2 ,3};
     
      System.out.println("Original Array : \n");
      for (int i = 0; i < org.length; i++)
           System.out.print(org[i] + " ");  
        
       // copying array org to copy 
       // Here, new array has 5 elements - two
       // elements more than the original array
       int[] copy = Arrays.copyOf(org, 5);
    
       System.out.print("\nNew array copy (of higher length):\n");
       for (int i = 0; i < copy.length; i++) 
           System.out.print(copy[i] + " ");
    }
}

Output:



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 contribute@geeksforgeeks.org. 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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :