Java Array multidimensional Arrays index

Question

What is the output of the following code?

public class Main {
  public static void main(String[] args) {
    int[][] array = { { 1, 2, 3, 4 }, 
                      { 5, 6, 7, 8 } /*  ww w.ja  v a2 s. co  m*/
                    };
    System.out.println(m1(array)[0]);
    System.out.println(m1(array)[1]);
  }

  public static int[] m1(int[][] m) {
    int[] result = new int[2];
    result[0] = m.length;
    result[1] = m[0].length;
    return result;
  }
}


2
4

Question

What is the output of the following code?

 Code:
 public class Main {
                                   // w ww  .  j  a  v a 2 s  .c o  m
   public static void main(String[] args) {
     int[][] array = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
     for (int i = array.length - 1; i >= 0; i--) {
       for (int j = array[i].length - 1; j >= 0; j--)
         System.out.print(array[i][j] + " ");
       System.out.println();
     }
   }
 }


6 5 
4 3 
2 1

Question

What is the output of the following code?

public class Main {

  public static void main(String[] args) {
    int[][] array = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
    int sum = 0;/*from  w w  w.  java2  s.  c  om*/
    for (int i = 0; i < array.length; i++)
      sum += array[i][0];
    System.out.println(sum);
  }
}


9



PreviousNext

Related