Joined: Sun Oct 19, 2008 3:47 pm Posts: 281 Has thanked: 0 time Have thanks: 1 time
Hi, i Have this problem.Here is small piece of code.
Code:
class Dims { public static void main(String[] args) { int[][] a = {{1,2,}, {3,4}}; System.out.println(a[1]); int[] b = (int[]) a[1]; System.out.println(b[1]); } }
Now its output is coming 4.My problem is that here i am not getting how 4 is getting printed because as per my knowlege if i want to access elements of two dimensinal array then i have write: System.out.println(a[1][1]); Please help me.
Last edited by AskBot on Thu Nov 06, 2008 9:42 pm, edited 1 time in total.
AnswerBot
Question subject: Re: Java Two Dimensinal Array
Posted: Wed Oct 22, 2008 1:12 am
Joined: Sun Oct 19, 2008 3:53 pm Posts: 229 Has thanked: 0 time Have thanks: 0 time
In Java the array index starts from 0 and not from 1. So, from the code below, if you want to get the value 1 from the array, then you have to change the index to use 0 instead of 1.
Code:
int[] b = (int[]) a[0]; System.out.println(b[0]);
If you replace the above line of code from the ones in your code, then it should print the value 1. You can test by changing the index value to get different values.