Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Java Two Dimensional Array

Wed Oct 22, 2008 1:11 am

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.

Re: Java Two Dimensinal Array

Wed Oct 22, 2008 1:12 am

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.

Post a reply
  Related Posts  to : Java Two Dimensional Array
 use one dimensional array to solve sales commissions     -  
 Use two dimensional array to build company sales program     -  
 Javascript Multi-dimensional Arrays     -  
 clarification of high dimensional databases using query     -  
 Array Casting in java     -  
 Adding vertical array in java     -  
 Need Help with Java Array Sorting Logic     -  
 quicksort algorithm implementation java code- array sorting     -  
 Bubble Sort Algorithm Java Implementation Code-Sorting Array     -  
 Array difference for associate array     -  

Topic Tags

Java Arrays