• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List<Integer> list = Arrays.asList(3, 2, 1, 0); list.remove(3); giving runtime exception

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



It gives run time exception..

But array values are converted as a list by using asList, then why cant we remove any value from a list???

Why this Kolaveri ???
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API mentions that remove() method can throw UnsupportedOperationException is the List doesn't support remove. And Arrays.asList according to the documentation: "Returns a fixed-size list backed by the specified array." and hence these operations would not be supported on the List thus obtained
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to have a list that you can later edit, then create a new ArrayList that will copy the content of the list returned by Arrays.asList(...):
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:The API mentions that remove() method can throw UnsupportedOperationException is the List doesn't support remove. An Arrays.asList according to the documentation: "Returns a fixed-size list


Ah, I remember the good old days when we would simply respond, "RTFM."
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:
It gives run time exception..


You were lucky this time--somebody knew what the problem was and was able to answer easily. In general though, you'll get better help if you TellTheDetails(⇐click). In this case, pasting in the exact, complete error message and indicating clearly which line caused it.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Vishal Hegde wrote:
It gives run time exception..


You were lucky this time--somebody knew what the problem was and was able to answer easily. In general though, you'll get better help if you TellTheDetails(⇐click). In this case, pasting in the exact, complete error message and indicating clearly which line caused it.

Exactly and I did run this program to find out what the runtime exception was and then referred the API for details and to see if there's anything mentioned there.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:Why this Kolaveri ???

Hmmm. wonderful.. for that you need to look in to java.util.AbstractList#add(int, Object)
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add, please be a little careful in using remove function. remove(3) will not remove the value 3 from the list.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic