簡體   English   中英

檢索2D數組的列大小?

[英]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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM