why the following makes ArrayIndexOutOfBoundsException [0]
Hi,
I think you are better than me in oop so you could easily show my error in a oop. My Code makes an error ArrayIndexOutOfBoundsException [0].
Where is my fault?
class C{
A a;
C(){
a =new A();
}
}
class A{
B []b;
A(){
b =new B[0];
b[0] =new B();
}
}
class B{
}
thanks
[851 byte] By [
Isaaka] at [2007-11-26 20:43:03]

# 1
> I think you are better than me in oop
Probably not, but I'm certainly better than you at electing the right forum to post a question :o)
Yours has nothing to do with OOP and patterns, and merely has its place in Java Programming (http://forum.java.sun.com/forum.jspa?forumID=31), or New to Java (http://forum.java.sun.com/forum.jspa?forumID=54).
> My Code makes an error
> ArrayIndexOutOfBoundsException [0].
Is that a trick question or what?
// create an array of 0 positions.
> b = new B[0];
// Try to access the first position; 1 > 0 so throws AIOOBE
> b[0] = new B();
# 2
b has no elements, but you're trying to access the first one.If you want b to have one element, then new B[1] (gives b[0])If you want it to have two elements, then new B[2] (b[0], b[1).etc. http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html