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








Syntax

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

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

Example

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

/*from w  w  w  .ja va 2 s.  c o m*/

import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    
    // intializing an array arr1
    long[] arr1 = new long[] {1L, 10L, 45L};


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

    // copying array arr1 to arr2 with newlength as 5
    long[] arr2 = Arrays.copyOf(arr1, 5);
    arr2[3] = 12L;
    arr2[4] = 123456789L;


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

The code above generates the following result.