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
Joined: Aug 05, 2005
Posts: 4
posted
0
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
Joined: Oct 16, 2005
Posts: 1277
posted
0
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
SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ] Performance is a compulsion, not a option, if my existence is to be justified.
Karu Raj
Ranch Hand
Joined: Aug 31, 2005
Posts: 479
posted
0
ho yes thanks for explanation
Naresh Gunda
Ranch Hand
Joined: Oct 15, 2005
Posts: 163
posted
0
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 ]