Can't add to an ArrayList “misplaced construct(s)”

后端 未结 5 1662
孤城傲影
孤城傲影 2020-12-11 23:02

I have a simple arraylist set up, but I can\'t seem to add objects to it.

import java.util.ArrayList;


public class Inventory {

ArrayList inventory = new A         


        
相关标签:
5条回答
  • 2020-12-11 23:17

    A more general answer would be that the class body is about declarations, not statements. There is special provision for statements occurring in class body, but they have to be marked explicitly as either class initializers or instance initializers. As the assignments are statements and statements are allowed only inside blocks of code(methods, constructors, static initializers, etc.). So you can do as below,

    public class Inventory {
    
    ArrayList inventory = new ArrayList();
    
    String item1 = null;
    String item2 = null;
    String item3 = null;
    
    //initializer block
    {
    item1="Sword";
    item2="Potion";
    item3="Shield";
    
    inventory.add(item1);
    inventory.add(item2);
    inventory.add(item3);
    }
    
    0 讨论(0)
  • 2020-12-11 23:21

    In Java, you cannot have executable statements, such as invocations of the add method, outside a definition of a method or a constructor. Declarations are OK, but executable statements are not.

    You can add these items to a named constructor, but you can also use an anonymous initialization block, like this:

    public class Inventory {
    
        ArrayList inventory = new ArrayList();
    
        {   // An anonymous initialization block
            String item1 = "Sword";
            String item2 = "Potion";
            String item3 = "Shield";
    
            inventory.add(item1);
            inventory.add(item2);
            inventory.add(item3);
        }
    }
    

    If you use a block like that, it would be shared among all named constructors of the class, or it would become part of the implicitly generated constructor for the Inventory.

    0 讨论(0)
  • 2020-12-11 23:25

    Your "adding" statements are just "in the air": not inside one method or constructor.

    Wrap in inside one like for instance:

    public class Inventory {
    
       private List inventory = new ArrayList();  //prefer interface here ;)
    
       public Inventory(){   //statements wrap into this constructor
          inventory.add("Sword");
          inventory.add("Potion");
          inventory.add("Shield");
       }
    }
    
    0 讨论(0)
  • 2020-12-11 23:30

    Non declaring code has to be in a block statement like the main method. If you would like to run the code e.g. in Eclipse with a debugger with right mouse click on the class and choosing Debug as/Java Application you could do it like this:

    import java.util.ArrayList;
    
    public class Inventory {
    
    private List<String> inventoryItems = new ArrayList<String>();
    
    public Inventory(){
        String item1 = "Sword";
        String item2 = "Potion";
        String item3 = "Shield";
    
        inventoryItems.add(item1);
        inventoryItems.add(item2);
        inventoryItems.add(item3);
    }
    
    public static void main(String[] args) {
        Inventory theInventory = new Inventory();
    }
    
    }
    

    This way you can set a break point at the left border of the Eclipse editing area and step through the code.

    Edit: As it looks to me like Adam experiments with the code I provided a solution that makes experimenting easier.

    0 讨论(0)
  • 2020-12-11 23:34

    The reason why your code does not work is that you tried to write code in the class body. Executable statements should be written in static initializers, methods or constructors (as I did in the example below).

    Try this:

    public class Inventory {
    
        private List inventory = new ArrayList();
    
        public Inventory() {
    
            String item1 = "Sword";
            String item2 = "Potion";
            String item3 = "Shield";
    
            inventory.add(item1);
            inventory.add(item2);
            inventory.add(item3);
        }
    }
    

    I defined the class member inventory in the class body and initialized it in-place (= new ArrayList();). No compiler error there because declarations are allowed in class body. The rest of the code I put into the constructor that will initialize inventory with values. I could have put it in a method, but I chose the constructor because its usual role is to initialize class members.

    0 讨论(0)
提交回复
热议问题