Home > Net >  Printing the 2nd values in 2d Array
Printing the 2nd values in 2d Array

Time:05-07

What change do I need to make in the following code

public void demo() {
int[][] num = {{0, 30}, {5, 10}, {15, 20}};

for(int i=0;i<num.length;i  ){
   // System.out.println(num[i][0]); 
   for(int j=0;j<num[i].length;j  ){
      System.out.println(num[j][i]);
   }
}

}

So that I can get 30,10,20 ? I want to find 0,5,15 which num[i][0] is doing. I am not sure what change to make just to get the 30,10,20

I do not want to use num[i][j] to get the pair values.

Thanks in advance for your time.

CodePudding user response:

Try this:

public void demo() {
    int[][] num = {{0, 30}, {5, 10}, {15, 20}};

    for(int i=0;i<num.length;i  ){
       System.out.println(num[i][1]); 
    }
}

CodePudding user response:

For get 30,10,20 write:

for (int i = 0; i < num.length; i  ) {
     System.out.println(num[i][1]);

For get 0,30,5,10,15,20 write:

for (int i = 0; i < num.length; i  ) {
     for (int j = 0; j < num[i].length; j  ) {
          System.out.println(num[i][j]);
     }
}
  • Related