Steveman30290
Steveman30290

Reputation: 561

Printing a Specific Column in a 2D Java Array

I have accessor method, where a user submits a specific column from a 2D array that they want printed out. However, I am having trouble printing out the specific column.

For example:

public array[] column(int col)
{ 

} 

And the 2D array is something like this:

5 5 5 5 5 0
8 5 2 5 5 5
5 5 5 5 1 5

So if somebody selects col = 3, it should print (5,5,5)

Upvotes: 0

Views: 4530

Answers (1)

rajashekar
rajashekar

Reputation: 689

you should know no of rows in the array you need to iterate through rows and the column value should be fixed

int noOfRows, int noOFColmuns;

public void printColumn(int[][] arr, int col){
     for(int i = 0 ; i < noOfRows; i++){
     System.out.println(arr[i][col]);
}

Upvotes: 1

Related Questions