Java array length double

Description

Java array length double

import java.util.Arrays;

public class Main {
  public static void main (String args[]) {
    int array1[] = {1, 2, 3, 4, 5};

    System.out.println("Original size: " + array1.length);
    System.out.println(Arrays.toString(array1));
    //from  w w  w .  j  a va2  s  .  c om
    array1 = doubleArraySize(array1);
    
    System.out.println("New size: " + array1.length);
    System.out.println(Arrays.toString(array1));
    

  }
  public static int[] doubleArraySize(int original[]) {
    int length = original.length;
    int newArray[] = new int[length*2];
    System.arraycopy(original, 0, newArray, 0, length);
    return newArray;
  }
}



PreviousNext

Related