Initialize array

This java example will demonstrate how to initialize a primitive or object array of specified type and size while using java and guava.

Straight up Java

Java core library provides two basic ways to initialize an array. First is to create an array with the new operator then assign a value to each element of the array. The second is to use the shortcut syntax to create and initialize an array.

Initialize primitive array

@Test
public void initalize_int_array_java () {

    // declares an array of integers
    int[] nflNorthStadiumsCapacity;

    // allocates memory for 4 integers
    nflNorthStadiumsCapacity = new int[4];

    // initialize elements
    nflNorthStadiumsCapacity[0] = 80750;
    nflNorthStadiumsCapacity[1] = 61500;
    nflNorthStadiumsCapacity[2] = 64121;
    nflNorthStadiumsCapacity[3] = 65000;

    assertTrue(nflNorthStadiumsCapacity.length == 4);
}

Initialize primitive array shortcut

@Test
public void initalize_int_array_java_shortcut () {

    int[] nflNorthStadiumsCapacity = {
            80750, 61500,
            64121, 65000};

    assertTrue(nflNorthStadiumsCapacity.length == 4);
}

Initialize object array

@Test
public void initialize_string_array_java () {

    // declares an array of strings
    String[] nflNorthStadiums;

    // allocates memory for 4 strings
    nflNorthStadiums = new String[4];

    // initialize elements
    nflNorthStadiums[0] = "Lambeau Field";
    nflNorthStadiums[1] = "Soldier Field";
    nflNorthStadiums[2] = "Mall of America Fielddagger";
    nflNorthStadiums[3] = "Ford Fielddagger";

    assertTrue(nflNorthStadiums.length == 4);
}

Initialize object array shortcut

@Test
public void initialize_string_array_java_shortcut () {

    // declares an array of strings
    String[] nflNorthStadiums = {
            "Lambeau Field",
            "Soldier Field",
            "Mall of America Fielddagger",
            "Ford Fielddagger"};

    assertTrue(nflNorthStadiums.length == 4);
}

Google Guava

ObjectArrays is a guava utility class that pertains to object arrays. In first example below, we will call the ObjectArrays.newArray method which will return a new array of the specified length and component type. The second example will call the overloaded ObjectArrays.newArray which will return a new array of the the given length and a reference type. In this case returning a String[].

Initialize object array

@Test
public void initialize_string_array_java_with_guava () {

    String[] nflNorthStadiums = ObjectArrays.newArray(String.class, 4);

    nflNorthStadiums[0] = "Lambeau Field";
    nflNorthStadiums[1] = "Soldier Field";
    nflNorthStadiums[2] = "Mall of America Fielddagger";
    nflNorthStadiums[3] = "Ford Fielddagger";

    assertTrue(nflNorthStadiums.length == 4);
}

Initialize object array w/ reference type

@Test
public void initialize_string_array_java_with_guava_reference_type () {

    String[] nflStadiums = {""};

    String[] nflNorthStadiums = ObjectArrays.newArray(nflStadiums, 4);

    nflNorthStadiums[0] = "Lambeau Field";
    nflNorthStadiums[1] = "Soldier Field";
    nflNorthStadiums[2] = "Mall of America Fielddagger";
    nflNorthStadiums[3] = "Ford Fielddagger";

    assertEquals(4, nflNorthStadiums.length);
}