How would I get just the second row to print in this 4x4 array?
double [][] table = new double[4][4];
for(int i = 0; i < table.length; i++){
for(int j = 0; j < table[i].length; j++)
table[i][j] = (Math.random() * 10);
}
How would I get just the second row to print in this 4x4 array?
double [][] table = new double[4][4];
for(int i = 0; i < table.length; i++){
for(int j = 0; j < table[i].length; j++)
table[i][j] = (Math.random() * 10);
}
Use this
// table[0] == 1st row
// table[1] == 2nd row
// etc..
for(int i = 0; i < table[1].length; i++)
System.out.println(table[1][i]); // Print each item of the 2nd row
Depending on the usage, you may want to use a method to print a specific row. Something like this
public void printRow(int r){
for(int i=0; i<table[r-1].length; i++){
if(i>0){
System.out.print(", ");
}
System.out.print(table[r-1][i]);
}
}
In this example, you would call printRow(2); when you want to print the 2nd row.