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 Arrays Creation Declaring Arrays

Todd Baker
Todd Baker
17,207 Points

Why use arrays when ArrayLists exist?

Why would I ever use an array when I could just use an ArrayList? Doesn't an ArrayList have more functionality like the ability to change its size?

2 Answers

Tonnie Fanadez
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

Hi Todd Baker

Good Question

Personally I will go with ArrayList because I can change the size at any time and also ArrayLists are really fast

However, to save memory Plain Arrays can be used when you have a fixed number of elements you’re sure this number is never going to change. ArrayLists usually allocate about twice the memory you need in advance so that you can append items very fast. So there is a wastage of memory if you are never going to add any more items.

Arrays also help to store data in multiple rows and columns also called multi-dimensional arrays

If you need a fixed number of elements or dealing with multiple dimensions, I suggest a plain array. If you need a simple list with lots of inserts and removals then Arraylist is the way to go.

But even with that, you can still simultaneously enjoy the strength of an Array and the strength of an ArrayList because Java allows you to convert from an Array to an ArrayList and vice-versa.

I hope this helps.. Merry coding!

Todd Baker
Todd Baker
17,207 Points

Thanks for the help! That makes a lot of sense.