• 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

Arrays......

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here when i compile i got runtime exceptions.

We know that arrays are given default value >.......

then it is failing ???

public class TestDogs
{
public static void main (String[] args)
{
TestDogs [] [] [] theDogs = new TestDogs [3][2][];
System.out.println(theDogs[2][0][0].toString());
}
}
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here the TestDogs[2][0][0] object doesn't exist.
And what's more,the subarray that the TestDogs[2][0] reference points to even hasn't be constructed.
So there's no way it'll turn out right.
 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java allows you to create arrays without mentioning the right most dimenesion. so the folloing line works fine

TestDogs [] [] [] theDogs = new TestDogs [3][2][];

but in line below you are accessing elements that have yet to be created

System.out.println(theDogs[2][0][0].toString());

the above line will work if u use something like this
theDogs[3][0]=new int[any size here];
theDogs[3][1]=new int[any size here];
theDogs[3][2]=new int[any size here];
//then this will work fine
System.out.println(theDogs[2][0][0].toString());

hope that helps
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ho yes thanks for explanation
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


TestDogs [] [] [] theDogs = new TestDogs [3][2][];
System.out.println(theDogs[2][0][0].toString());

the above line will work if u use something like this
theDogs[3][0]=new int[any size here];
theDogs[3][1]=new int[any size here];
theDogs[3][2]=new int[any size here];



Hi Niranjan,
In the array declaration [3][2][] is given. Here 3 is the no. of two dimensioal arrays. so index range is 0-2 only know. theDogs[3][0] may give ArrayIndexOutOfBounds Exception, I think.
[ December 27, 2005: Message edited by: Naresh Kumar ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic