简体   繁体   中英

Getting a compiling error: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList

I am developing a textbased RPG. In one of the classes there is an ArrayList that handles save and load game. There is both Integers and Strings in the ArrayList.

This is a String: saveGame.add(p.getName());
And this is a Integer: saveGame.add(p.getCurrLoc());

Every variable that is adding an element to the list receive a warning like this one:

D:\\Backup\\RPG\\src\\rpg\\Player.java:86: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList getInventory().add(item); where E is a type-variable: E extends Object declared in class ArrayList

Code:

public class Data {

private ArrayList saveGame;

//Sparar en ny rad i sparfilen
public void writeData(String in) {        

    try {
        FileWriter out = new FileWriter("Savegame.txt", true);
        out.write(in + "\n");
        out.close();
    } catch (IOException ex) {
        Logger.getLogger(Data.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public void save(Player p) {
    clearData();


    saveGame = new ArrayList();
    saveGame.add(p.getName());                  
    saveGame.add(p.getCurrLoc());               
    saveGame.add(p.getStrength());              
    saveGame.add(p.getConstitution());         
    saveGame.add(p.getDexterity());             
    saveGame.add(p.getHitpoints());            
    saveGame.add(p.getLevel());                
    saveGame.add(p.getxP());                    
    saveGame.add(p.getxPToLvlUp());             
    saveGame.add(p.getArmorClass());           
    saveGame.add(p.getNrOfPotions());           
    saveGame.add(p.getEqWeapon().getName());   


    for(Object element : p.getInventory()) {
        if(element instanceof Weapon) {
            saveGame.add(((Weapon) element).getName());
        } else if(element instanceof Item) {
            saveGame.add(((Item) element).getName());
        } else if(element instanceof Armor) {
            saveGame.add(((Armor) element).getName());
        }
    }

    for(Object element : saveGame) {
        writeData(element.toString());
    }
}

First of all, this is a warning, not an error. You can still run your code in this state. It just opens the door to many potential problems.

Basically, you should define your list so that you know what type each and every element is. This is done using generics.

So you can declare your list as a list of strings as follows:

ArrayList<String> saveGame = new ArrayList<String>();

This means you can only add strings to the list. However, it also means that getter methods will always return strings, so no casting is ever necessary.

The latest versions of Java allow you to specify the type(s) of object that you will place in your ArrayList using a syntax like this ArrayList. This allows the compiler to check the types of any object you add to the list at compile and execution time, and allows for safer and less error-prone code

If you use the legacy versions as you have done (without the ), then the compiler cannot make this check so it gives you a warning.

(Posted on behalf of the OP) .

I solved the problem like this:

ArrayList<Object> saveGame = new ArrayList<Object>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM