0

i was today on class and i had to basically make a program that ask you cases, a string (That i had to make a list with it) and count how many times something that is not "Eclipse" appears (On the case of the exercise they were games and "LoL" counts 2, not 1).

But at the moment of clearing the "activity" list the program trows java.lang.UnsupportedOperationException and i don't know why

package arrayList;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Arrays;
import java.util.List;

public class ShitList {

    public static void main(String[] args) {
        
        //Variables generals / Controladors
        Scanner kyb = new Scanner(System.in);
        List<String> activity = new ArrayList<String>();
        ArrayList<Integer> spy = new ArrayList<Integer>();
        
        int shitList = 0;
        
        //Variables usuari
        int laps = 0;
        int hours = 0;
        
        String usr = "";
        
        //Demanem les els casos al usuari
        
        laps = kyb.nextInt();
        kyb.nextLine();
        
        for (int i = 0; i < laps; i++) {
            
            //Demanem la "Activity list"
            usr = kyb.nextLine();
            
            //Declarem un array temporal
            String [] activitySplit;
            
            //La guardem en un array
            activitySplit = usr.split(" ");

            //Pasem la array a arrayList
            activity = Arrays.asList(activitySplit);

            //Demanem quantes hores el Marc espia al Albert
            hours = kyb.nextInt();
            
            for (int x = 0; x < hours; x++) {
                
                spy.add(kyb.nextInt());
            }
            
            for (int x = 0; x < hours; x++) {
                
                if(spy.get(x) == 15) {
                    
                    if (activity.get(0).equals("LoL")) {
                        
                        shitList++;
                    }
                    
                    else if (!activity.get(0).equals("Eclipse")) {
                    
                        shitList++;
                    }
                    
                }else if (spy.get(x) == 16) {
                    
                    if (activity.get(1).equals("LoL")) {
                        
                        shitList++;
                    }
                    
                    else if (!activity.get(1).equals("Eclipse")) {
                    
                        shitList++;
                    }
                    
                }else if (spy.get(x) == 17) {
                    
                    if (activity.get(2).equals("LoL")) {
                        
                        shitList++;
                    }
                    
                    else if (!activity.get(2).equals("Eclipse")) {
                    
                        shitList++;
                    }
                    
                }else if (spy.get(x) == 18) {
                    
                    if (activity.get(3).equals("LoL")) {
                        
                        shitList++;
                    }
                    
                    else if (!activity.get(3).equals("Eclipse")) {
                    
                        shitList++;
                    }
                    
                }else if (spy.get(x) == 19) {
                    
                    if (activity.get(4).equals("LoL")) {
                        
                        shitList++;
                    }
                    
                    else if (!activity.get(4).equals("Eclipse")) {
                    
                        shitList++;
                    }
                    
                }else if (spy.get(x) == 20) {
                    
                    if (activity.get(5).equals("LoL")) {
                        
                        shitList++;
                    }
                    
                    else if (!activity.get(5).equals("Eclipse")) {
                    
                        shitList++;
                    }
                    
                }else if (spy.get(x) == 21) {
                    
                    if (activity.get(6).equals("LoL")) {
                        
                        shitList++;
                    }
                    
                    else if (!activity.get(6).equals("Eclipse")) {
                    
                        shitList++;
                    }
                    
                }
            }
            
            System.out.println("Nivell de Shitlist: " + shitList);
            
            /*Clears all the variables to start the next case, but for some reason, trows Exception in thread "main" java.lang.UnsupportedOperationException
            at java.base/java.util.AbstractList.remove(AbstractList.java:167)
            at java.base/java.util.AbstractList$Itr.remove(AbstractList.java:387)
            at java.base/java.util.AbstractList.removeRange(AbstractList.java:598)
            at java.base/java.util.AbstractList.clear(AbstractList.java:243)
            at arrayList.ShitList.main(ShitList.java:143)
             */
            
            activity.clear();
            spy.clear();
            shitList = 0;
            
        }
        
        kyb.close();
    }

}

I know this code is a bit caothic, sorry.

  • 1
    List created via `Arrays.asList(...)` has fixed size. You can't *add* or *remove* elements from it, which includes *clearing* it (although you can *replace* elements with `set` method). You may want to wrap it in `new ArrayList<>(Arrays.asList(..))` which creates resizable list. – Pshemo Nov 17 '21 at 20:16

0 Answers0