Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, July 28, 2016

Can't add to an ArrayList

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


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 ArrayList();    String item1 = "Sword";  String item2 = "Potion";  String item3 = "Shield";    inventory.add(item1);  inventory.add(item2);  inventory.add(item3);  }  

There are two errors, one at the dot between inventory and add, and one at the variable names between the brackets, being

Syntax error on token(s), misplaced construct(s)  

and

Syntax error on token "item1", VariableDeclaratorId expected after this token  

Can anyone explain why this is happening?

Answer by darijan for Can't add to an ArrayList "misplaced construct(s)"


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.

Answer by dasblinkenlight for Can't add to an ArrayList "misplaced construct(s)"


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.

Answer by Mik378 for Can't add to an ArrayList "misplaced construct(s)"


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");     }  }  

Answer by Sandro for Can't add to an ArrayList "misplaced construct(s)"


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 inventoryItems = new ArrayList();    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.

Answer by Rohan Salunkhe for Can't add to an ArrayList "misplaced construct(s)"


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);  }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.