Java Arrays - copyOf() Method



Description

The Java Arrays copyOf(short[] original,int newLength) 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(short[] original,int newLength) method

public static short[] copyOf(short[] original,int newLength)

Parameters

  • original − This is the array to be copied.

  • newLength − This is the length 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.

Example 1

The following example shows the usage of Java Arrays copyOf() method. First, we've created an array of shorts. 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) {
      short[] shortArr = { 10, 20, 30, 13 };
      System.out.print("Short Array: [");
      for (int i = 0; i < shortArr.length; i++) {
         System.out.print(shortArr[i] + " ");
      }
      System.out.print("]\nCopied Array: \n");

      // Create copy of the array of same size
      short[] shortArrCopy = Arrays.copyOf(shortArr, shortArr.length);     
      System.out.print("Short Array: [");
      for (int i = 0; i < shortArrCopy.length; i++) {
         System.out.print(shortArrCopy[i] + " ");
      }
      System.out.print("]");
   }
}

Output

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

Short Array: [10 20 30 13 ]
Copied Array: 
Short Array: [10 20 30 13 ]

Example 2

The following example shows the usage of Java Arrays copyOf() method. First, we've created an array of shorts. 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) {
      short[] shortArr = { 10, 20, 30, 13 };
      System.out.print("Short Array: [");
      for (int i = 0; i < shortArr.length; i++) {
         System.out.print(shortArr[i] + " ");
      }
      System.out.print("]\nCopied Arrays: \n");

      // Create copy of the array of greater size
      short[] shortArrCopy = Arrays.copyOf(shortArr, shortArr.length + 1);
      System.out.print("Short Array: [");
      for (int i = 0; i < shortArrCopy.length; i++) {
         System.out.print(shortArrCopy[i] + " ");
      }
      System.out.print("]");
   }
}

Output

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

Short Array: [10 20 30 13 ]
Copied Array: 
Short Array: [10 20 30 13 0 ]

Example 3

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

package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
   public static void main(String[] args) {
      short[] shortArr = { 10, 20, 30, 13 };
      System.out.print("Short Array: [");
      for (int i = 0; i < shortArr.length; i++) {
         System.out.print(shortArr[i] + " ");
      }
      System.out.print("]\nCopied Arrays: \n");

      // Create copy of the array of lesser size
      short[] shortArrCopy = Arrays.copyOf(shortArr, shortArr.length - 1);
      System.out.print("Short Array: [");
      for (int i = 0; i < shortArrCopy.length; i++) {
         System.out.print(shortArrCopy[i] + " ");
      }
      System.out.print("]");
   }
}

Output

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

Short Array: [10 20 30 13 ]
Copied Array: 
Short Array: [10 20 30 ]
java_util_arrays.htm
Advertisements