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.
答案 0 :(得分:0)
您需要重新构建edges
数组。这更适合3d阵列。 (或者,更好的说法,一组2d数组)
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.