| Author |
Collections - Exception
|
Alexsandra Carvalho
Ranch Hand
Joined: Jul 13, 2007
Posts: 75
|
|
Hi, Why is there a java.lang.UnsupportedOperationException at the line 3??
|
 |
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8712
|
|
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
|
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
|
 |
Alexsandra Carvalho
Ranch Hand
Joined: Jul 13, 2007
Posts: 75
|
|
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
Joined: Dec 28, 2007
Posts: 2212
|
|
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.
|
Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
|
 |
Alexsandra Carvalho
Ranch Hand
Joined: Jul 13, 2007
Posts: 75
|
|
Very good Roberto . Thank you!!
|
 |
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8712
|
|
|
Good job Alexsandra!
|
 |
camilo lopes
Ranch Hand
Joined: Aug 08, 2007
Posts: 202
|
|
|
because the method asList return a String? this correct?
|
Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
|
 |
Roberto Perillo
Bartender
Joined: Dec 28, 2007
Posts: 2212
|
|
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
Joined: Oct 13, 2005
Posts: 32599
|
|
|
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
Joined: Dec 28, 2007
Posts: 2212
|
|
|
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!
|
 |
 |
|
|
subject: Collections - Exception
|
|
|