0

First here is the code of the class that is bringing me trouble (its a class defined in a class):

public static class MAbilitiesParseStruct{
    private List<CardView> cards;
    private List<String> abilities;
    private List<Boolean> ask;
    private List<Integer> regtype; //Used for regitering events.
    //private List<Integer> pos;
    private int current = 0;

    //public MAbilitiesParseStruct(List<CardView> cvs, List<String> abs, List<Boolean> asks, List<Integer> p){
    public MAbilitiesParseStruct(List<CardView> cvs, List<String> abs, List<Boolean> asks, List<Integer> regt){
        cards = cvs;
        abilities = abs;
        ask = asks;
        regtype = regt;
        //pos = p;
        current = 0;
    }

    public List<CardView> getCardList(){return cards;}
    public List<String> getAbilities(){return abilities;}
    public List<Boolean> getAsk(){return ask;}
    public List<Integer> getRegTypes(){return regtype;}

    public void addAbility(CardView v, String ability, boolean askq, int rtype){
        cards.add(v);
        abilities.add(ability);
        ask.add(askq);
        regtype.add(rtype);
    }

    public void appendAnotherMaps(MAbilitiesParseStruct another){
        cards.addAll(another.getCardList());
        abilities.addAll(another.getAbilities());
        ask.addAll(another.getAsk());
        regtype.addAll(another.getRegTypes());
    }

    public boolean done(){if (current == cards.size()) return true; else return false;}
    public void next(){ //It is assumed that this is called once current ability has finished being parsed.         
        if (cards.size() > 0){
            cards.remove(0);
            abilities.remove(0);
            ask.remove(0);
            regtype.remove(0);              
        }
    }

    public void purgeOneShots(){
        int i = 0;
        System.err.println("Purge de 1 shot");
        while (i < cards.size()){
            if (regtype.get(i) == Aux.REG_ONESHOT){
                System.err.println("Purge card");
                cards.remove(i);
                System.err.println("Purge ability");
                abilities.remove(i);
                System.err.println("Purge ask");
                ask.remove(i);
                System.err.println("Purge regtype");
                regtype.remove(i);                      
            }
            else{
                i++;
            }
        }
    }

    public void purgeTurns(){
        int i = 0;
        while (i < cards.size()){
            if (regtype.get(i) == Aux.REG_TURN){
                cards.remove(i);
                abilities.remove(i);
                ask.remove(i);
                regtype.remove(i);                      
            }
            else{
                i++;
            }
        }
    }       

    public String currentAbility(){return abilities.get(current);}
    public boolean currentAsk(){return ask.get(current);}
    public CardView currentCard(){return cards.get(current);}
    public void reset() {current = 0;}

    public void clearRemainingEvents(){ //This is used for when a certain card cancels an event.
        for (int i = current+1; i < cards.size(); i++){
            cards.remove(i);
            abilities.remove(i);
            ask.remove(i);
            regtype.remove(i);              
        }
    }




}

And here is the invocation code that throws the exception (RegisteredAbilities is a SparseIntArray):

    MAbilitiesParseStruct temp = RegisteredAbilities.get(event,null);       
    if (temp != null){
        MAPS.appendAnotherMaps(temp);
        RegisteredAbilities.get(event).purgeOneShots();
    }

The code fails with unsupported operation for remove on the last line and I can't undersand why. Is it becuase I'm calling the operation from an SparseIntArray?

Thanks for any help

EDIT:

As requested here is the stack trace.

01-20 18:45:28.010: E/AndroidRuntime(29696): FATAL EXCEPTION: main
01-20 18:45:28.010: E/AndroidRuntime(29696): java.lang.UnsupportedOperationException
01-20 18:45:28.010: E/AndroidRuntime(29696):    at java.util.AbstractList.remove(AbstractList.java:638)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at auxdata.AuxClasses$MAbilitiesParseStruct.purgeOneShots(AuxClasses.java:209)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at legen.dary.GameEngine.LaunchEvent(GameEngine.java:611)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at legen.dary.GameEngine.newVillainEntered(GameEngine.java:319)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at legen.dary.GameEngine.GameTurn(GameEngine.java:193)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at legen.dary.MainActivity.LoadGamePressed(MainActivity.java:290)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at legen.dary.ButtonBlockView$1.onClick(ButtonBlockView.java:54)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at android.view.View.performClick(View.java:4084)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at android.view.View$PerformClick.run(View.java:16966)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at android.os.Handler.handleCallback(Handler.java:615)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at android.os.Looper.loop(Looper.java:137)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at android.app.ActivityThread.main(ActivityThread.java:4745)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at java.lang.reflect.Method.invokeNative(Native Method)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at java.lang.reflect.Method.invoke(Method.java:511)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-20 18:45:28.010: E/AndroidRuntime(29696):    at dalvik.system.NativeStart.main(Native Method)
6
  • 3
    Could you please post the exact exception with its stacktrace? Commented Jan 20, 2014 at 22:00
  • Whatever implementation of List that is being passed into your object and assigned to regType doesn't support the remove() operation. Commented Jan 20, 2014 at 22:29
  • @BrianRoach I don't think so. According to the trace it fails in the CardView. The implementations ar Allways ArrayList... But I have other classes with CardView lists and they all work Commented Jan 20, 2014 at 23:17
  • I'am truly sorry. After searching the code I came across a place where the variable put int eh RegisterEvent Variable was created from Arrays.asList and that is causing the problem. Thanks for the help. Commented Jan 20, 2014 at 23:30
  • Rarely does the JVM lie to you ;) I was guessing that or something similar was the issue. Commented Jan 20, 2014 at 23:52

1 Answer 1

1

From the API:

Arrays.asList: Returns a fixed-size list backed by the specified array.

You can't add to it; you can't remove from it. You can't structurally modify the List.

Fix

Create a LinkedList, which supports faster remove.

List<String> list = new LinkedList<String>(Arrays.asList(split));
Sign up to request clarification or add additional context in comments.

1 Comment

This is an old question, but it deserved the answer anyways. It was my mistake. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.