GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Conversion of Array into List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Conversion of Array into List" Watch "Conversion of Array into List" New topic
Author

Conversion of Array into List

Pradeep Balasubramanian
Ranch Hand

Joined: Jun 16, 2008
Posts: 75
For the code provided below,



why do we get the Runtime Exception.

while all the methods of List Interface are accessible by the List(converted
from Array), why is it not possible to add any new elements into the new Converted List.

Please Explain....


Regards,<br />Pradeep Balasubramanian<br /> <br />You have to grow from the inside out. None can teach you, none can make you spiritual. There is no other teacher but your own soul. <br /> <br />- Swami Vivekananda
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
If you goto the API, ou would find this:
Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)
So this means that the list is fixed-size and hence you cannot add to it.
However the list is synchronized with the array, this means that if you change any element in the array, same would be reflected in the list.
You can do this by:


Preparing for SCJP 6.0
Pradeep Balasubramanian
Ranch Hand

Joined: Jun 16, 2008
Posts: 75
Now I am clear

Also,
Once the length of array is specified , its size cannot be changed later.

Thank you, for pointing the error made by me.

[ September 16, 2008: Message edited by: Pradeep Balasubramanian ]
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
That is so obvious.
When you say:


You define the size of the array at compile time and cannot change it at runtime. Thats is what lists are for.
 
GeeCON Prague 2014
 
subject: Conversion of Array into List