Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays......

 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • 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());
}
}
 
bibby young
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
ho yes thanks for explanation
 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic