Two Laptop Bag*
The moose likes Java in General and the fly likes List<Integer> list = Arrays.asList(3, 2, 1, 0); list.remove(3); giving runtime exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "List<Integer> list = Arrays.asList(3, 2, 1, 0); list.remove(3); giving runtime exception" Watch "List<Integer> list = Arrays.asList(3, 2, 1, 0); list.remove(3); giving runtime exception" New topic
Author

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

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055




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 ???


http://www.lifesbizzare.blogspot.com || OCJP:81%
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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


Mohamed Sanaulla | My Blog
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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(...):


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Sep 08, 2007
Posts: 3068
    
  33

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jul 03, 2011
Posts: 256
Just to add, please be a little careful in using remove function. remove(3) will not remove the value 3 from the list.
 
 
subject: List<Integer> list = Arrays.asList(3, 2, 1, 0); list.remove(3); giving runtime exception
 
Similar Threads
Unnecessary cast in List?
java.util.List method addAll(int,Collection)?
Behavior of Arrays.asList()
Question of Arrays.asList()
Collections (LinkedHashMap)