This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections - Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections - Exception" Watch "Collections - Exception" New topic
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: 8801
    
    5
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


Spot false dilemmas now, ask me how!
(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: 2258
    
    3

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: 8801
    
    5
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: 2258
    
    3

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: 37879
    
  22
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: 2258
    
    3

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!
 
wood burning stoves
 
subject: Collections - Exception
 
Similar Threads
Swap sublist of an arraylist with the sublist of another arraylist
asList ..unexpected output
Prototype and script.aculo.us ToC
Help needed in Generics toArray() method
converting Arrays to List?