Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Abhinav Kanoria
Abhinav Kanoria
7,730 Points

Creating empty array

At 8;29 in the video, Craig created an empty array Treet[] treets = new Treet[0]. After that, he extracted the two Treet objects from the file treets.ser and put them in the array. How come we can add stuff to an array when we declare it to be of size 0? Please explain. Thanks!

1 Answer

Kristian Gausel
Kristian Gausel
14,661 Points

That must be a typo or something, because like you are saying it can't be done:

        Object[] t = new Object[0];

        t[0] = new Object();

Gives an java.lang.ArrayIndexOutOfBoundsException