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++;
}
}
}
}
How do I get Java to retrieve the number of columns once it reaches a row, so I can use that as a base case for the inner for loop?
There are supposed to be 12 columns, ranging from 0 to 11. Unfortunately the outer loop iterates 46 times. The inner loop always iterates 2 times, meaning the program things there are 46 rows with 2 columns to each row.
How can I get java to think the two dimensional array has 12 rows with the following column amount displayed for each of those rows:
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.
You need to restructure your edges
array. This is better suited for a 3d array. (Or, better spoken, an array of 2d arrays)
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.");
}
This gives me the following output:
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.