-6

I want to know the array representaion of this code,

int []x[]={{1,2},{3,4,5},{6,7,8,9}};

The program is:

 class Testsss 
 {
    public static void main(String[] args) 
    {
       int []x[]={{1,2},{3,4,5},{6,7,8,9}};
       int [][]y=x;
       System.out.println(y[2][1]);
    }
  }

Output is 7, as I executed. How can the values are represented in array form.

SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
Aravind KR
  • 27
  • 6
  • 3
    Please ask a clear question. – Manu Feb 15 '16 at 13:23
  • Array start from 0 not from 1, but I am not sure that is the answer for your question... – amkz Feb 15 '16 at 13:25
  • The question is how the values given in x variable is represented in an array form. – Aravind KR Feb 15 '16 at 13:26
  • int[2][2] x is represnted as 00 01 10 11 [Matrix form]. As given the values in x varaible in program, the values are not clear, so as to represent in array form – Aravind KR Feb 15 '16 at 13:27
  • Maybe you want this System.out.println(Arrays.deepToString(y)); Not sure I understand the actual question – anaxin Feb 15 '16 at 13:30
  • 1
    Well, arrays are, if you want so, some type of matrix, but are not necesserly represantable as a matrix (especially if the arrays are of different size). A two dimensional array is basicly just an array of arrays. In this case, the first array would be of the size 2, the second of the size 3, the fourth of the size 4. – SomeJavaGuy Feb 15 '16 at 13:30

1 Answers1

0

Maybe you are looking for something like this:

public class Test {
    public static void main(String[] args) {
        int []x[]={{1,2},{3,4,5},{6,7,8,9}};
        for(int i=0; i<x.length; i++) {
            for(int j=0; j<x[i].length; j++) {
                System.out.println("x["+i+"]["+j+"]= " + x[i][j]);
            }
        }
    }
}

the output is:

x[0][0]= 1
x[0][1]= 2
x[1][0]= 3
x[1][1]= 4
x[1][2]= 5
x[2][0]= 6
x[2][1]= 7
x[2][2]= 8
x[2][3]= 9
pleft
  • 7,567
  • 2
  • 21
  • 45