Welcome to the Treehouse Community
Looking to learn something new?
Creating Lists with Arrays.asList() vs new ArrayList()
I posted this on the Sets lesson, but I think here would be more appropiate. Sorry for the double post, but I am freaking out with this question =D
I still don't understand when to use the following methods for creating lists. Is there a preference or a difference? The output on java-repl seems to be the same.
List<String> list1 = new ArrayList<String>(); list1.add("foobar");
List<String> list2 = Arrays.asList("foobar");
Is the Arrays.asList() method preferred for creating lists with a length greater than one?
I'm not an expert, so take my answer with the appropriate level of salt...
I believe that the Arrays.asList() method is primarily intended to allow a programmer to work with pre-existing arrays that subsequently need to be modified. It was basically a required "work-around" due to the immutable nature of arrays. If you are creating a new list, it is probably a better idea to stick with ArrayList.