Total members 11889 |It is currently Tue Apr 16, 2024 8:36 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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.

Author:
Proficient
User avatar Posts: 280
Have thanks: 1 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.

_________________
Please recommend my post if you found it helpful


Author:
Proficient
User avatar Posts: 228
Have thanks: 0 time
Post new topic Reply to topic  [ 2 posts ] 

  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     -  
 quicksort algorithm implementation java code- array sorting     -  
 Bubble Sort Algorithm Java Implementation Code-Sorting Array     -  
 Array difference for associate array     -  
 Sort strings java-Sorting Array of Strings     -  



Topic Tags

Java Arrays






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com