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;
}
}