• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why this code does not generate any exception?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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}};
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java does not support multi-dimensional arrays.
http://www.xdweb.net/~dibblego/java/faq/answers.html#q45
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[IDLE CURIOSITY]
how does a non-rectangular "2D" array work?
[\IDLE CURIOSITY]
 
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic