File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections :List.add throws runtime Exception(Unsupported Exceptions) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections :List.add throws runtime Exception(Unsupported Exceptions)" Watch "Collections :List.add throws runtime Exception(Unsupported Exceptions)" New topic
Author

Collections :List.add throws runtime Exception(Unsupported Exceptions)

Suresh Babu Venkat
Ranch Hand

Joined: Sep 15, 2009
Posts: 30
please see the code given below



i am getting the following Exception during runtime

element 0::test0
element 1::test1
element 2::test2
element 3::test3
element 4::test4
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:131)
at java.util.AbstractList.add(AbstractList.java:91)
at ListTest.main(ListTest.java:24)

After seeing all the javadocs , what i understood is Arrays.asList returns a fixedsize list backed by array .

is this Exception has anything to do with this Fixed Size?

what i am thinking is as it returns a fixed size list we are not allowed to add new elements to it ,please tell me whether i am correct or not .If this is the case , wouldn't it throw some other exception?

sorry if this is a silly question.

Thanks in advance


SCJP 6.0 (88%) | SCWCD 5.0 (89%)
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Hey Suresh,

It seems like a reasonable exception to me, are you thinking there is a more suitable one?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

It's fixed size list, you can replace(update) with new elements rather than add new elements.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sajith Hasanka
Greenhorn

Joined: Aug 20, 2009
Posts: 17
You cannot perform <Collection>.add or <Collection>.addAll operation on collections you have obtained from Arrays.asList .. only remove operation is permitted ..

Same applies for the collections obtained via Map.values() operation..

Thanks
Sajith


- SCJP 6.0
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Hi all,

What i think is, Array has fixed-size. and when we retrieve list from array using Arrays.asList(...), it returns the arrays element in list format and, both array and list points to the same object.
If we try to add new element in list, it means we try to add element in array also which is fixed-size. so it get exception.

please, correct me if i am wrong..

Thanks,
Ankit


OCPJP 6 (91%)
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

Ankit Gareta wrote:
What i think is, Array has fixed-size. and when we retrieve list from array using Arrays.asList(...), it returns the arrays element in list format and, both array and list points to the same object.


Not literally, but yes you are correct. The new list object will act as "bridge" and propagate any changes made (to the list) to the array object.

FWIW, Java 7 is intelligent to use the SAME list object how many ever times the Arrays.asList() is called using the same array object.

- Rakesh


Warm Regards,
Rakesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections :List.add throws runtime Exception(Unsupported Exceptions)