0

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);
}
5
  • You want to print this array after storing the data? Commented Dec 18, 2015 at 11:13
  • Yes, but just what's stored in the second row Commented Dec 18, 2015 at 11:14
  • I know how to print the entire array, I'm just having problems with the logic on specifically getting just a specific row Commented Dec 18, 2015 at 11:15
  • Do you want just the fourth row or is it the fourth COLUMN you want? Commented Dec 18, 2015 at 11:23
  • I need to just print the row Commented Dec 18, 2015 at 11:33

3 Answers 3

1

Use this

for(int j = 0; j < table[1].length; j++)
   System.out.print(table[1][j]+" - ");
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

This did the trick. I just needed to remove the second for loop
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.