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 trialbehar
10,799 PointsWhy would you EVER use arrays in java?
Arrays as compared to lists just seem to worthless. In my expierence an array that is immutable is a useless array.
Im ofcourse new to programming, so my question is, do you often find yourself using arrays in java? And is it good practise to use them if i dont need to modify their length, or is it good to just always use lists?
2 Answers
Jonathan Grieve
Treehouse Moderator 91,253 PointsI think just use arrays to begin with as it introduces you to the concept of storing multiple items in a collection. I'm sure arrays will be useful to you at some point but as you go through your journey you'll find them to be kind of the little brother to other collection types like lists and sets. :)
Daniel Hartin
5,311 PointsHi Behar,
Arrays definitely have their uses especially if you are tight on resources. I totally get where you are coming from but behind the Collections framework for Lists such as ArrayList it is essentially an array. When you use List.add() the framework creates a new array with an extra space than the current limit of your array and adds a space to it and then essentially copies all this data across and appends the new item to it. This is done very efficiently but it is still not as efficient as creating your own fixed length array and adding to it.
Where i've found arrays are really useful is when trying to model a spreadsheet of fixed data for example. You could use a List of objects but say you simply have 20 float values as columns why not create a List of float arrays with a fixed size of 20, when I've dealt with 100's MB's or GB's of data and it's really efficent to hold it in RAM to process instead of writing to and from the ROM being efficent can save you lots and lots of time :).