Java Collection Tutorial - Java Arrays.copyOf(double[] original, int newLength)








Syntax

Arrays.copyOf(double[] original, int newLength) has the following syntax.

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

Example

In the following code shows how to use Arrays.copyOf(double[] original, int newLength) method.

/*www.j  a va 2  s .  com*/
import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
   
    // intializing an array arr1
    double[] arr1 = new double[] {1.5, 2.5, 3.5};

    System.out.println(Arrays.toString(arr1));

    // copying array arr1 to arr2 with newlength as 5
    double[] arr2 = Arrays.copyOf(arr1, 5);
    arr2[3] = 0.44;
    arr2[4] = 0.55;
   
    System.out.println(Arrays.toString(arr2));
  }
}

The code above generates the following result.