wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays...... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays......" Watch "Arrays......" New topic
Author

Arrays......

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
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

Joined: Aug 05, 2005
Posts: 4
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
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
ho yes thanks for explanation
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays......