简体   繁体   中英

How do I read (i.e. understand) this Java array?

String[][][] arr  = 
{{ { "a", "b" , "c"}, { "d", "e", null } },{ {"x"}, null },{{"y"}},{ { "z","p"}, {} }};

I've reason to believe that something like this is going to come up on an exam eventually but in +10 years of coding, I've never seen such a horror. I sure could use some tips for mentally parsing this, so that I can figure out what it really means in my head. Sure, I could run it through IntelliJ but I won't have it available.

For the record, this makes perfect sense to me:

int[][] twoD = { { 1, 2, 3} , { 4, 5, 6, 7}, { 8, 9, 10 } };

I recommend adding some indentation, which will help you keep track of what's at what level of this nested array:

String[][][] arr  =  {
    {
        { "a", "b", "c" },
        { "d", "e", null }
    },
    {
        { "x" },
        null
    },
    {
        { "y" }
    },
    {
        { "z", "p" },
        {}
    }
};

Does that help? I find this makes it easier to visualize the data structure and to answer questions like "What's the value of arr[1][0][0] ?"

You can read the type String[][][] as "an array of arrays of arrays of strings," or perhaps "a three-dimensional array of strings."

Maybe you will understand more from this, i can see your array like this :

String[] arr0 = {"a", "b", "c"};
String[] arr1 = {"d", "e", null};
String[] arr2 = {"x"};
String[] arr3 = null;
String[] arr4 = {"y"};
String[] arr5 = {"z", "p"};
String[] arr6 = {};

String[][] arr0_1 = {arr0, arr1};//-->>  {{"a", "b", "c"}, {"d", "e", null}}
String[][] arr2_3 = {arr2, arr3};//-->>  {{"x"}, null}
String[][] arr4_4 = {arr4};      //-->>  {{"y"}}
String[][] arr5_6 = {arr5, arr6};//-->>  {{"z", "p"}, {}}

String[][][] arr = {arr0_1, arr2_3, arr4_4, arr5_6};

which is equivalent to

String[][][] arr
        = {
            {//----------------------------
                {"a", "b", "c"},//arr0    |
                                        //|---->arr0_1
                {"d", "e", null}//arr1    |
            },//---------------------------

            {//----------------------------
                {"x"},//arr2              |
                                        //|---->arr2_3
                null  //arr3              |
            },//---------------------------

            {//----------------------------
                {"y"}//arr4               |---->arr4_4
            },//---------------------------

            {//----------------------------
                {"z", "p"},//arr5         |
                                        //|---->arr5_6
                {}//arr6                  |
            }//---------------------------
        };

Formatting the array will help to figure out that as a matrix of rows and cols

String[][][] arr  = 
{
    {{"a", "b" , "c"}, {"d", "e", null} },
    {{"x"           }, null             },
    {{"y"           }                   },
    {{"z", "p"      }, {                }}
};

2 Cols, 4 rows

element at 0,0: {"a", "b" , "c"}

element at 0,1: {"d", "e", null}

element at 1,0: {"x"}

element at 1,1: null

etc etc

String[][][] arr = {

            // arr[0]
            {
                    /* arr[0][0] */
                    {
                            /* arr[0][0][0] */ "a",
                            /* arr[0][0][1] */ "b",
                            /* arr[0][0][2] */ "c"
                    },

                    /* arr[0][1] */
                    {
                            /* arr[0][1][0] */ "d",
                            /* arr[0][1][1] */ "e",
                            /* arr[0][1][2] */ null
                    }
            },

            // arr[1]
            {
                    /* arr[1][0] */
                    {
                            /* arr[1][0][0] */ "x"
                    },

                    /* arr[1][1] */
                    null
            },

            // arr[2]
            {
                    /* arr[2][0] */
                    {
                            /* arr[2][0][0] */ "y"
                    }
            },

            // arr[3]
            {
                    /* arr[3][0] */
                    {
                            /* arr[3][0][0] */ "z",
                            /* arr[3][0][1] */ "p"
                    },

                    /* arr[3][1] */
                    {}
            }
    };

Building on @smarx answer:

// [][][] is an array of 2D arrays
String[][][] arr = { // begin declaration of arr
    // first 2D array
    {
        {"[0][0][0]", "[0][0][1]", "[0][0][2]"},
        {"[0][1][0]", "[0][1][1]", null, "[0][1][3]"}
    },
    // second 2D array
    {
        {"[1][0][0]", "[1][0][1]"},
        null // we should check for this when reading
    },
    // third 2D array
    {
        {"[2][0][0]", "[2][0][1]", "[2][0][2]"}
    },
    // fourth 2D array
    {
        {"[3][0][0]", "[3][0][1]"},
        {}
    },
    // fifth 2D array
    {
        {"[4][0][0]", "[4][0][1]", "[4][0][2]"},
        {"[4][1][0]", "[4][1][1]"},
        {"[4][2][0]", "[4][2][1]", "[4][2][2]"}
    }   
};

// Reading the 3D array, as: [x][y][z]
for (int x = 0; x < arr.length; x++) {
    for (int y = 0; y < arr[x].length; y++) {
        // one of our 2D arrays has a null array, 
        // declared in the line: {"[0][1][0]", "[0][1][1]", null, "[0][1][3]"}
        if (arr[x][y] != null) {
            for (int z = 0; z < arr[x][y].length; z++) {
                System.out.println(arr[x][y][z]);
            }
        } else {
            System.out.println("null 2D array");
        }
    }
}

We must check for the null arrays because we are trying to access something and it doesn't exist. The null string doesn't need to be checked (for printing) because Java will already make this check when printing, see why null reference prints as "null"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM