简体   繁体   English

在2D Java数组中打印特定列

[英]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. 我有访问器方法,其中用户从2D数组提交他们想要打印的特定列。 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: 2D数组是这样的:

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) 因此,如果有人选择col = 3,则应打印(5,5,5)

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]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM