Java Arrays copyOf() Method



Description

The Java Arrays copyOf(U[] original, int newLength, Class<? extends T[]> newType) method copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain false. Such indices will exist if and only if the specified length is greater than that of the original array.

Declaration

Following is the declaration for java.util.Arrays.copyOf(T[] original,int newLength) method

public static <T,​U> T[] copyOf​(U[] original, int newLength, Class<? extends T[]> newType)

Type Parameters

  • T − This is the class of the objects in the returned array.

  • U − This is the class of the objects in the original array.

Parameters

  • original − This is the array to be copied.

  • newLength − This is the length of the copy to be returned.

  • newType − This is the class of the copy to be returned.

Return Value

This method returns a copy of the original array, truncated or padded with false elements to obtain the specified length.

Exception

  • NegativeArraySizeException − If newLength is negative.

  • NullPointerException − If original is null.

  • ArrayStoreException − if an element copied from original is not of a runtime type that can be stored in an array of class newType.

Copying an Array of Objects to a Same Sized Array Example

The following example shows the usage of Java Arrays copyOf() method. First, we've created an array of Monitor objects. We've printed them. A copy of array with same size is created using copyOf() method and printed.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      Student[] studentArr = { new Monitor(1, "Julie", false), new Monitor(2, "Robert", true), new Monitor(3, "Adam", false) };

      System.out.print("Student Array: [");
      for (int i = 0; i < studentArr.length; i++) {
         System.out.print(studentArr[i] + " ");
      }
      System.out.print("]\nCopied Arrays: \n");

      // Create copy of the array of same size
      Monitor[] studentArrCopy = Arrays.copyOf(studentArr, studentArr.length, Monitor[].class);

      System.out.print("Student Array: [");
      for (int i = 0; i < studentArrCopy.length; i++) {
         System.out.print(studentArrCopy[i] + " ");
      }
      System.out.print("]");
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

class Monitor extends Student{

   boolean priviledges;

   Monitor(int rollNo, String name, boolean priviledges) {
      super(rollNo, name);
      this.priviledges = priviledges;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + ", " + this.priviledges + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] [ 3, Adam, false ] ]
Copied Arrays: 
Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] [ 3, Adam, false ] ]

Copying an Array of Objects to a Higher Sized Array Example

The following example shows the usage of Java Arrays copyOf() method. First, we've created an array of Monitor objects. We've printed them. A copy of array with higher size is created using copyOf() method and printed with default values appended.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      Student[] studentArr = { new Monitor(1, "Julie", false), new Monitor(2, "Robert", true), new Monitor(3, "Adam", false) };

      System.out.print("Student Array: [");
      for (int i = 0; i < studentArr.length; i++) {
         System.out.print(studentArr[i] + " ");
      }
      System.out.print("]\nCopied Arrays: \n");

      // Create copy of the array of greater size
      Monitor[] studentArrCopy = Arrays.copyOf(studentArr, studentArr.length + 1, Monitor[].class);

      System.out.print("Student Array: [");
      for (int i = 0; i < studentArrCopy.length; i++) {
         System.out.print(studentArrCopy[i] + " ");
      }
      System.out.print("]");
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

class Monitor extends Student{

   boolean priviledges;

   Monitor(int rollNo, String name, boolean priviledges) {
      super(rollNo, name);
      this.priviledges = priviledges;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + ", " + this.priviledges + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] [ 3, Adam, false ] ]
Copied Arrays: 
Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] [ 3, Adam, false ] null ]

Copying an Array of Objects to a Lower Sized Array Example

The following example shows the usage of Java Arrays copyOf() method. First, we've created an array of Monitor objects. We've printed them. A copy of array with lower size is created using copyOf() method and printed.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      Student[] studentArr = { new Monitor(1, "Julie", false), new Monitor(2, "Robert", true), new Monitor(3, "Adam", false) };

      System.out.print("Student Array: [");
      for (int i = 0; i < studentArr.length; i++) {
         System.out.print(studentArr[i] + " ");
      }
      System.out.print("]\nCopied Arrays: \n");

      // Create copy of the array of lesser size
      Monitor[] studentArrCopy = Arrays.copyOf(studentArr, studentArr.length - 1, Monitor[].class);

      System.out.print("Student Array: [");
      for (int i = 0; i < studentArrCopy.length; i++) {
         System.out.print(studentArrCopy[i] + " ");
      }
      System.out.print("]");
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

class Monitor extends Student{

   boolean priviledges;

   Monitor(int rollNo, String name, boolean priviledges) {
      super(rollNo, name);
      this.priviledges = priviledges;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + ", " + this.priviledges + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] [ 3, Adam, false ] ]
Copied Arrays: 
Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] ]
java_util_arrays.htm
Advertisements