检索二维数组的列大小?

Retrieve columns size of a 2D array?

public class TwoD_Array_Column_Length {

public static void main(String[] args) {

    int numEdges = 0;

    String[] vertices = {   "Seattle", "San Francisco", "Los Angeles", "Denver",    // 0 , 1 , 2 , 3
                            "Kansas City", "Chicago", "Boston", "New York",         // 4 , 5 , 6 , 7
                            "Atlanta", "Miami", "Dallas", "Houston" };              // 8 , 9 , 10, 11

    // Edge array has 46 total pairs
    int[][] edges = { 
            { 0, 1 }, { 0, 3 }, { 0, 5 },                                       // row 0/vertex 0
            { 1, 0 }, { 1, 2 }, { 1, 3 }, 
            { 2, 1 }, { 2, 3 }, { 2, 4 }, { 2, 10 }, 
            { 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 4 }, { 3, 5 }, 
            { 4, 2 }, { 4, 3 }, { 4, 5 }, { 4, 7 }, { 4, 8 }, { 4, 10 }, 
            { 5, 0 }, { 5, 3 }, { 5, 4 }, { 5, 6 }, { 5, 7 }, 
            { 6, 5 }, { 6, 7 }, 
            { 7, 4 }, { 7, 5 }, { 7, 6 }, { 7, 8 }, 
            { 8, 4 }, { 8, 7 }, { 8, 9 }, { 8, 10 }, { 8, 11 }, 
            { 9, 8 }, { 9, 11 }, 
            { 10, 2}, { 10, 4 }, { 10, 8 },{ 10, 11 }, 
            { 11, 8 }, { 11, 9 },{ 11, 10 } };


    // row < edges.length, this causes 92 iterations 
    //      double the value of 46 pairs, whis is wrong
    for(int row = 0; row < edges.length; row++) {

        System.out.println("------ \t------ \t------ \t------");
        System.out.println("------ \t------ \t------ \t------");
        System.out.println("Row " + row + " has " + (edges[row].length + 1) + " columns.");

        System.out.println("\nWith Array.toString method: ");
        System.out.println("Row "+ row + " has value " 
                + java.util.Arrays.toString(edges[row]) + " as pairs.\n");

        // This inner loop will iterate based on number of column each row has
        for(int col = 0; col < edges[row].length; col++) {  // 'column'         

            System.out.println("******ENTER Inner loop******");
            System.out.println("Current Row holds the value: " + row 
                    +  "\n   While its column holds the value: " + edges[row][col]);

            System.out.println("******LEAVE Inner loop******");
            numEdges++;
        }
    }       
}
}    

如何让 Java 在到达一行后检索列数,以便我可以将其用作内部 for 循环的基本情况?

应该有 12 列,范围从 0 到 11。不幸的是,外循环迭代了 46 次。内部循环总是迭代 2 次,这意味着程序有 46 行,每行 2 列。

我怎样才能 java 认为二维数组有 12 行,每行显示以下列数量:

Row 0: 3 columns. 
Row 1: 3 columns. 
Row 2: 4 columns. 
Row 3: 5 columns. 
Row 4: 6 columns. 
Row 5: 5 columns. 
Row 6: 2 columns. 
Row 7: 4 columns. 
Row 8: 5 columns. 
Row 9: 2 columns. 
Row 10: 4 columns. 
Row 11: 3 columns. 

您需要重组 edges 阵列。这更适合 3d 阵列。 (或者,更好地说,二维数组的数组)

 int[][][] edges = { 
            {{ 0, 1 }, { 0, 3 }, { 0, 5 }},                                       // row 0/vertex 0
            {{ 1, 0 }, { 1, 2 }, { 1, 3 }}, 
            {{ 2, 1 }, { 2, 3 }, { 2, 4 }, { 2, 10 }}, 
            {{ 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 4 }, { 3, 5 }}, 
            {{ 4, 2 }, { 4, 3 }, { 4, 5 }, { 4, 7 }, { 4, 8 }, { 4, 10 }}, 
            {{ 5, 0 }, { 5, 3 }, { 5, 4 }, { 5, 6 }, { 5, 7 }}, 
            {{ 6, 5 }, { 6, 7 }}, 
            {{ 7, 4 }, { 7, 5 }, { 7, 6 }, { 7, 8 }}, 
            {{ 8, 4 }, { 8, 7 }, { 8, 9 }, { 8, 10 }, { 8, 11 }}, 
            {{ 9, 8 }, { 9, 11 }}, 
            {{ 10, 2}, { 10, 4 }, { 10, 8 },{ 10, 11 }}, 
            {{ 11, 8 }, { 11, 9 },{ 11, 10 } }};


for(int row = 0; row < edges.length; row++){
    System.out.println("Row " + row + ": " + edges[row].length + " columns.");
}

这给了我以下输出:

Row 0: 3 columns.
Row 1: 3 columns.
Row 2: 4 columns.
Row 3: 5 columns.
Row 4: 6 columns.
Row 5: 5 columns.
Row 6: 2 columns.
Row 7: 4 columns.
Row 8: 5 columns.
Row 9: 2 columns.
Row 10: 4 columns.
Row 11: 3 columns.