Can anyone tell me why this method is throwing an unchecked or unsafe exception? I understood that to happen when I try to edit the list I am iterating through... in the below, currentAdvanceTactics etc are not being iterated through at all.

    public void tacticMeetsRequirements(ArrayList<Tactics> master) {

        for (Tactics a : master) {
            if (meetsrequirements(a)) {
                if (a.movement.equals("Advance")) {
                currentAdvanceTactics.add(a);                   
                } else if (a.movement.equals("Retreat")) {
                currentRetreatTactics.add(a);   
                } else if (a.movement.equals("Guard")) {
                currentGuardTactics.add(a); 
                }
            }       
        }

    }

This is how the objects used in the master list are created:

    for (int b = 0; b < numberoftactics; b++) {
         tactic[b] = new Tactics(parsedTactics[b]); 
         tacticsMaster.add(tactic[b]);
    }

parsedTactics is just raw data which gets read into the different variables.

tacticsMaster is declared as such:

public ArrayList<Tactics> tacticsMaster;

then later when I create the object it is contained within:

this.tacticsMaster = new ArrayList<Tactics>();

The currentAdvanceTactics list etc are all created as such:

    public ArrayList currentGuardTactics = new ArrayList<Tactics>();
    public ArrayList currentAdvanceTactics = new ArrayList<Tactics>();
    public ArrayList currentRetreatTactics = new ArrayList<Tactics>();

Thanks in advance for any help!

有帮助吗?

解决方案

You are using the raw version of the generic-type (ArrayList<T>). That's why you get the warning.

public ArrayList currentGuardTactics = new ArrayList<Tactics>();
public ArrayList currentAdvanceTactics = new ArrayList<Tactics>();
public ArrayList currentRetreatTactics = new ArrayList<Tactics>();

Try using the parameterized version -

public List<Tactics> currentGuardTactics = new ArrayList<Tactics>();
public List<Tactics> currentAdvanceTactics = new ArrayList<Tactics>();
public List<Tactics> currentRetreatTactics = new ArrayList<Tactics>();

其他提示

The declaration of your ArrayLists are not type-safe. Change it to:

private ArrayList<Tactics> currentGuardTactics = new ArrayList<Tactics>();
private ArrayList<Tactics> currentAdvanceTactics = new ArrayList<Tactics>();
private ArrayList<Tactics> currentRetreatTactics = new ArrayList<Tactics>()

You should make the members also private. public declarations are usually not the best-practice way.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top