Initialize part of an array in java

You can do something like this, it will create array with new size based on provided.

    String[] temp = new String[] {"water", "shovel", "berries", "stick", "stone", "seed", "axe"};
    String[] val = Arrays.copyOf(temp, 20);

    System.out.println(val.length);
    System.out.println(Arrays.toString(val));

The output will be:

20
[water, shovel, berries, stick, stone, seed, axe, null, null, null, null, null, null, n

Leave a Comment