• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why this code does not generate any exception?

 
Lalit Kapoor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Q {
public static void main(String argv[]){

int anar[]=new int[]{1,2,3};

System.out.println(anar[2]);

}

}

The output of this code is 3. But why? As there is no third row in the two dimensional array. What is the reason behind this?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lalit,

I don't see any two-dimensional arrays; "new int[]{1,2,3}" creates a one-dimensional array with three numbers in it. Creating a two-dimensional array with the bracket-initialization syntax would look like

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

or, more simply,

int anar[][] = {{1,2,3}, {4,5,6}, {7,8,9}};
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not support multi-dimensional arrays.
http://www.xdweb.net/~dibblego/java/faq/answers.html#q45
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that link, I am assured that
This distinction [that what might be mistakenly called multidemensional arrays are in fact arrays of arrays] is important in coming to terms with the Java programming language, and is not purely academic as some might suggest.


I am then presented with some pretty generic code using arrays of arrays, and informed that
It is important to realise, that in the above code sample, there is no "2 dimensional" array. There is an array whose elements are String arrays.


However, I am never told why this is important to realize. In what instance would this knowledge influence my use of arrays?
[ June 08, 2004: Message edited by: Joseph George ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference influences what you can and can do as far as initializing and working with arrays, but mostly "around the edges." The distinction is unimportant for most straightforward uses. As the web page points out, a "2D" array doesn't have to be rectangular, and this influences how nested loops should be written, but only rare code exploits this fact.

C(++) supports both kinds of arrays, but (a) doesn't support Java's shortcut syntax for initializing the array-of-pointers-to-arrays kind, and (b) requires you to deallocate the memory for the sub-arrays manually, so in that language the distinction is much more important.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[IDLE CURIOSITY]
how does a non-rectangular "2D" array work?
[\IDLE CURIOSITY]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic