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








Syntax

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

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

Example

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

/* w  ww  . j  ava  2s  .com*/
import java.util.Arrays;

public class Main {

  public static void main(String[] args) {

    
    // intializing an array arr1
    float[] arr1 = new float[] {10f, 20f, 25f};

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

    // copying array arr1 to arr2 with newlength as 5
    float[] arr2 = Arrays.copyOf(arr1, 5);
    arr2[3] = 30f;
    arr2[4] = 40f;   

    System.out.println(Arrays.toString(arr2));
 }
}

The code above generates the following result.