This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections - Exception

 
Alexsandra Carvalho
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Why is there a java.lang.UnsupportedOperationException at the line 3??



 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Alexsandra,

From the "teach a person to fish" department:

Did you read the API concerning this method? It is a little complicated, but for sure that's a great place to start! No matter what other study materials you're using, it's a good idea to get comfortable reading and using the API doc.

So, it would be great to see a question like:

"The API says that this method, blah, blah, blah and I tried X,Y, Z, so I'm confused about ABC"

hth,

Bert
 
Alexsandra Carvalho
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I read the API and I think the problem is that the asList() method return a List with a fixed-size, and, therefore, I can't add or remove any element.

Is that?
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy.

The problem is that this method "links" the list to the array. The size of the array is being set to 2 when you say {"1", "2"}. When you try to add something to the list that is linked to the actual array, you're trying to change the size of the array. You can't do that on the fly.

This code shows that the "asList(Object[] array)" method links the array to the list:



You're changing the array, and this reflects on the list.
 
Alexsandra Carvalho
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very good Roberto .
Thank you!!
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job Alexsandra!
 
camilo lopes
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because the method asList return a String? this correct?
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by camilo lopes:
because the method asList return a String? this correct?


Howdy.

No. The asList method returns a List. To be more specific, it returns an ArrayList, but since ArrayList implements List, then you can set the return to List (which is even better).
 
Campbell Ritchie
Sheriff
Posts: 48442
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right the the asList() method returns an ArrayList, but it is actually a different class from the ArrayList we are familiar with. It just happens to have the same name.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that you mentioned... I hadn't realized that the Arrays class has an internal static class called ArrayList (which also implements List). That's the one!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic