• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections - Exception

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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



 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very good Roberto .
Thank you!!
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good job Alexsandra!
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because the method asList return a String? this correct?
 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Marshal
Posts: 79232
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic