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 Exploring the Java Collection Framework Using ArrayLists

Kevin Faust
Kevin Faust
15,353 Points

some more info on lists

hi,

so in simple terms, basically lists are dynamic arrays and arrays are fixed arrays? and i dont know why anyone would use an array. lists are basically the same except much more flexible.

and are there any good beginer external resources on this stuff because i understand whats happening but its still pretty fuzzy.

and what was craig saying about at the end? im not sure but it was something about how we can add objects to lists but only their reference ? can anyone give a brief explanation of what craig said?

thank you

1 Answer

An ArrayList (and I am speaking just for it, I don't want to go in depth about a LInkedList, since that's more complicated) offers more functionality than an array itself. However, if you know exactly how big an array will be in compile time and don't need the functionality a list offers, it's better to go for it to prevent yourself from using list's so called overhead. Overhead is an extra memory usage/runtime cost that a given object/function/etc uses. In ArrayList's case that would be Type *array, int size, int capacity (those three variables are saved inside an ArrayList and you don't touch them or work with them, I'm just giving them for info)

Usually overhead isn't that bad but in like 90%+ of the cases you won't need to make the effort. Only when you really need to get maximum performance like scientific calculations or in some cases of game development. If you're going for some vector or something like it, it's a lot more convenient to make a class that has an array instead of using list. (By the way there are a libraries that do all that for you so no worries)

So the answer got a little messy but rest assure this all clears over time with PRACTICE. This is a lot more practice than theory. I can tell you different things, but you will have to see it all for yourself. So just keep on and with time it will be clearer.

tl;dr - Use arrays when you know exactly how big it will have to be in compile time (and if you don't need the list's functionality)