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 Java Data Structures Organizing Data Serialization

Kabir Gandhiok
Kabir Gandhiok
12,553 Points

Why do we create a 0 length treats array in the load method?

I cant seem to understand why a 0 length treats array is created in the load method? Appreciate it if anyone could explain this to me.. thanks!

Siyuan Ji
Siyuan Ji
3,651 Points

Same question here ! Can anyone explain why the 0 length array can be replaced by the new array? In previous course the instructor has mentioned that the size of an array cannot be modified, then how does it work here?

Same question. Don't understand how the length of the array can change to the length of the casted object array.

1 Answer

Pavle Delic
Pavle Delic
8,088 Points

Hi, 0 length array is created so if anything goes wrong with the rest of the code, method have something to return. And second concern is about array changing it's length... It really doesn't.

treets = (Treet[]) ois.readObject();

From this point in the code, "treets" doesn't point to the place in memory previously reserved. It is abandoned now, destroyed. This actually overwrites whatever "threets" was, with newly created array of treets ((Treet[]) ois.readObject()).