Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion of Array into List

 
Pradeep Balasubramanian
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Pradeep Balasubramanian
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic