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








Syntax

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

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

Example

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

import java.util.Arrays;
/*from  w w w .ja  v  a 2  s .c o m*/
public class Main {

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


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

    // copying array arr1 to arr2 with newlength as 5
    short[] arr2 = Arrays.copyOf(arr1, 5);
    arr2[3] = 100;
    arr2[4] = 200;


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

The code above generates the following result.