Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Prasoon Shukla3,426 Points
Array to List conversion
In Main.java, what is the purpose of doing: List<String> fakeResults = Arrays.asList() ?Why are we converting array as list? Can we not use a list directly here?
Grigorij Schleifer10,363 Points
I think the main reason why you convert an array into a List is because Lists are much easier to use and have much more functionality.
Array is static in size that is fixed length data structure, One can not change the length after creating the Array object. So if you have an array and it can hold 6 primitives and suddenly you want to add more then 6 items into it. You can´t ! You will need to create a bigger array and combine them together and so on .... ugly right :)
List is an interface in Java, which means that it may have multiple implementations. One of these implementations is ArrayList, which is a class that implements the behavior of the List interface using arrays as the data structure.
So if you convert an array into List and implement/add the functionality of an ArrayList for example, you can add items into this List/ArrayList and enjoy it. Because elements are added to an ArrayList its capacity grows automatically.
Here is a great comparison between ArrayList and array:
I hope i could help :)