Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

arrays

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friends,
Here goes the code,
class a{
public static void main(String args[]){
int a[][]= new [2][];
System.out.println(a[0][0]);
}}
Output;
null
In this case i suppose it should print 0. Why it doesn't happen? Would be happy if you give a simple example for three dimentional array.
thanks folks!!
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

class a{
public static void main(String args[]){
int a[][]= new [2][];
System.out.println(a[0][0]);
}}


First of all, I'm assuming that you're statement declaring the array was meant to be
int a[][] = new int [2][];
When I run this code, I get a null pointer exception, the reason being that a 2-dimensional array is an array of arrays. What you've declared so far is that you'll have 2 arrays that will contain arrays, but you haven't created or declared the second array.
If you're creating a rectangular array (both arrays hold the same number of elements, in this case 5), you could use:
int a[][] = new int[2][5];
If your array is non-rectangular, you can declare it in a format like this:
int a[][] = new int[2][];
a[0] = new int[5];
a[1] = new int[8];
So what you're declaring is that a is an array of two arrays, the first of which holds 5 integers, and the second of which holds 8 integers.
For more info and an example on 2 and 3-dimensional arrays, see my posting at http://www.javaranch.com/ubb/Forum33/HTML/001644.html
(and this is my first shot at using UBB tags, so pardon me if this doesn't show up quite the way I'd intended.)
 
sankar sivassoupramanime
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks buddy!!!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic