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.

Java

JJ Brandler
JJ Brandler
988 Points

converting arrays to lists

I have been working with the Treet class to try and familiarize myself with some of the concepts.

After Loading the treets.ser file into an array:

 Treet[] treets = Treets.load();

I created a list of treets and converted the array into a List and passed it into the list:

 List<Treet> lTreets = new ArrayList<Treet>();
 lTreets = Arrays.asList(treets);

Printed out how many treets were in the list:

 System.out.printf("There are %d treets in the list lTreets %n", lTreets.size());

Printed out a specific treet in the list:

 System.out.println(lTreets.get(25));

Created a new treet:

 Treet treet = new Treet("jj", "here's a new treet #treet @treehouse", new Date(1421878767000l));

That all worked fine. but when I tried to add the new treet into the list:

 lTreets.add(treet);

It did not work, and got an error message about an unsupported operation. A little bit of googling showed my the the implementation of a List as Arrays.asList() does not support the add() method.

Some more research shows that by initializing the List as :

 List<Treet> lTreets = new ArrayList<Treet>(Arrays.asList(treets));

will fix the problem, and it does.

So here are my questions:

  1. Does that cast the Array.asList as an ArrayList?
  2. Is there a way to convert the array directly to an ArrayList rather than doing it via the Arrays.asList
  3. How is List implemented as Arrays.asList different than an array

2 Answers

JJ, here's a quote from the Java API for Arrays.asList(): Returns a fixed-size list backed by the specified array.

In short, it's fixed size because it is backed by an array (which is immutable).

Seth Kroger
Seth Kroger
56,403 Points

Arrays.asList() doesn't return a java.util.ArrayList<>. It is a List<>, but it's an implementation that's wrapped around an existing array and has limitations because of that. The big one is that the list length is fixed and there is no way to add or remove items to it (because the backing array is a fixed size). The main benefit of Arrays.asList is in allows you to use the array where Lists are expected, but not arrays. (hmmm....Adapter pattern, perhaps)

What new ArrayList<Treet>(Arrays.asList(treets)) does is copy the Treets into a new ArrayList. Since there is a constructor that takes a Collection, but not one taking an array, you need to use Arrays.asList to make the array look like one.