有人可以帮助我解释为什么我无法添加到创建的任何一个列表中吗?仅创建第二个列表以说明问题。谢谢你的帮助!
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.Image;
public class Snake {
private Image head = new Image("data/head.png");
private static Image body = new Image("data/body.png");
ArrayList<Point> snakeCoords = new ArrayList<Point>(3);
Point headCoord = new Point(300, 300);
Point firstBody = new Point(300, 325);
Point secondBody = new Point(300, 350);
snakeCoords.add(headCoord);
List<String> list = new ArrayList<String>();
list.add("Does this work");
}
答案 0 :(得分:2)
如果您发布的代码准确无误,则需要将方法调用放在方法或构造函数中。你可以在方法之外初始化变量,但那就是它。