Print array

This example will show how to print an array of values for single and multidimensional arrays. Java Arrays class contains various methods, such as sorting and searching, to work with arrays. Below we will use two functionally similar methods, Arrays.toString for one dimensional and Arrays.deepToString for multi dimensional arrays, to capture a string representation of the contents of the array.

One Dimensional

@Test
public void print_array_in_java () {

    String[] legendsDivision = {
            "Michigan", "Michigan State", "Minnesota",
            "Northwestern", "Iowa", "Nebraska"};

    assertEquals("[Michigan, Michigan State, "
            + "Minnesota, Northwestern, Iowa, Nebraska]",
            Arrays.toString(legendsDivision));

}

Multi Dimensional

@Test
public void print_multidimensional_array_in_java () {

    String[][] big10Conference = {
            {"Michigan", "Michigan State", "Minnesota", "Northwestern", "Iowa", "Nebraska"},
            {"Ohio State", "Illinois", "Indiana", "Penn State", "Wisconsin", "Purdue"}
           };

    assertEquals ("[[Michigan, Michigan State, Minnesota, Northwestern, Iowa, Nebraska], "
            + "[Ohio State, Illinois, Indiana, Penn State, Wisconsin, Purdue]]",
            Arrays.deepToString(big10Conference));

}