Unable to add a String to an ArrayList: “misplaced construct(s)”
问题 I am trying to add a string to an ArrayList in Java, but I can't seem to get it to work. Currently, I have the following code: List food_names = new ArrayList(); food_names.add("pizza"); Why do I get these errors: Syntax error on token ""pizza"", delete this token Syntax error on token(s), misplaced construct(s) 回答1: You have to use food_names.add("pizza") in function, for example: public void AddFood() { food_names.add("pizza"); } Hope helps 回答2: why don't you use List Generics List