Java Arrays copyOf() Method



Description

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

public static char[] copyOf(char[] 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.

Copying an Array of chars to a Same Sized Array Example

The following example shows the usage of Java Arrays copyOf() method. First, we've created an array of chars. 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) {

      char[] charArr = { 'A', 'B', 'C', 'D' };

      System.out.print("Char Array: [");
      for (int i = 0; i < charArr.length; i++) {
         System.out.print(charArr[i] + " ");
      }

      System.out.print("]\nCopied Array: \n");

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

Output

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

Char Array: [A B C D ]
Copied Array: 
Char Array: [A B C D ]

Copying an Array of chars to a Higher Sized Array Example

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

package com.tutorialspoint;

import java.util.Arrays;

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

      char[] charArr = { 'A', 'B', 'C', 'D' };
      System.out.print("Char Array: [");
      for (int i = 0; i < charArr.length; i++) {
         System.out.print(charArr[i] + " ");
      }
      System.out.print("]\nCopied Arrays: \n");

      // Create copy of the array of greater size
      char[] charArrCopy = Arrays.copyOf(charArr, charArr.length + 1);

      System.out.print("Char Array: [");
      for (int i = 0; i < charArrCopy.length; i++) {
         System.out.print(charArrCopy[i] + " ");
      }
      System.out.print("]");
   }
}

Output

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

Char Array: [A B C D ]
Copied Array: 
Char Array: [A B C D   ]

Copying an Array of bytes to a Lower Sized Array Example

The following example shows the usage of Java Arrays copyOf() method. First, we've created an array of chars. We've printed them. A copy of array with lower 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) {

      char[] charArr = { 'A', 'B', 'C', 'D' };

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

      // Create copy of the array of lesser size
      char[] charArrCopy = Arrays.copyOf(charArr, charArr.length - 1);

      System.out.print("Char Array: [");
      for (int i = 0; i < charArrCopy.length; i++) {
         System.out.print(charArrCopy[i] + " ");
      }
      System.out.print("]");
   }
}

Output

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

Char Array: [A B C D ]
Copied Array: 
Char Array: [A B C ]
java_util_arrays.htm
Advertisements