0

This may be a duplicate, but I can't see any questions with this error, so Apologies if it is.

I'm trying to use the remove() method to remove an Integer from my ArrayList, however it is giving me java.lang.UnsupportedOperationException. The remove method should take a int or Integer to my understanding, or a value from the ArrayList, however these don't seem to work and give the same error.

I have also tried using "depth" as an index, as that is the index which I want to remove.

Here is my code:

import java.util.*;

public class EP{
public static List<Integer> items = Arrays.asList(12, 13, 48, 42, 38,     2827, 827, 828, 420);
public static void main(String[]args){
System.out.println("Exam List");
for(Integer i: items){
    System.out.println(i);
}
    Scanner scan = new Scanner(System.in);
System.out.println("Enter depth");
int depth = scan.nextInt();
System.out.println("Enter value");
int value = scan.nextInt();
System.out.println(mark(depth, value));
}

public static int  mark(int depth, int value){
int ret = -1; //This ensures -1 is returned if it cannot find it at the specified place
for(Integer i: items){
    if(items.get(depth) == (Integer)value){ //This assummes depth starts at 0
    ret = value;
    items.remove(items.get(depth)); // has UnsupportedOperationException
    }
    }
System.out.println("Updated Exam List");
for(Integer j: items){
    System.out.println(j);
}
return ret;
}
}
Ethan Moore
  • 383
  • 1
  • 5
  • 18
  • 1
    Please format your code. This is entirely illegible. – Boris the Spider May 04 '16 at 19:40
  • Arrays.asList() does not return ArrayList. I will like you to find if you can modify the list implementation returned by Arrays.asList (). see the difference between iterating using simple for loop, iterating using enhanced for loop and iterating using the List's iterator. Also see as when you can modify a List while iterating using Iterator or enhanced for loop and when you cannot do so. When you study these then you will be clear of all that you are doing in your code and also the exceptions you are gettig. In your case modification while iteration isn't reason for exception but study still – nits.kk May 04 '16 at 19:46
  • see [this](http://stackoverflow.com/a/158269/5394855) post about creating an `ArrayList` from an `Array`. – MercyBeaucou May 04 '16 at 20:09
  • @BoristheSpider, I'm new to this, It really is formatted, but somehow it uploaded it like that. – Rory Costello May 04 '16 at 21:00

1 Answers1

8

The List implementation returned by Arrays.asList is not java.util.ArrayList. It's a different implementation defined inside the Arrays class, which is a fixed sized List. Therefore you can't add/remove element to/from that List.

You can overcome this issue by creating a new java.util.ArrayList initialized by the elements of your List :

public static List<Integer> items = new ArrayList<>(Arrays.asList(12, 13, 48, 42, 38, 2827, 827, 828, 420));

That said, calling items.remove from within a loop that iterates over items using the enhanced for loop will not work (it will throw CuncurrentModificationException). You can use a traditional for loop instead (or an explicit Iterator if you want to remove the current element that the Iterator is pointing to, which doesn't seem to be the case).

Eran
  • 387,369
  • 54
  • 702
  • 768