简体   繁体   English

如何在类级别添加ArrayList?

[英]How to add an ArrayList at the class level?

I'm attempting to make a DnD character generator, and for part of that I need to have a list of stats that are editable by other methods, so I am attempting to add it at the class level. 我试图制作一个DnD字符生成器,​​对于其中的一部分,我需要有一个可由其他方法编辑的统计信息列表,因此,我试图在类级别添加它。 My current code is: 我当前的代码是:

public class CharacterCreator extends Application
{
    ArrayList<String> stats = new ArrayList<String>();

        stats.add("STR");
        stats.add("DEX");
        stats.add("CON");
        stats.add("INT");
        stats.add("WIS");
        stats.add("CHA");

public void start(Stage primaryStage)
{

But when I try to run it, I get a "identifier expected" error in on every 'add' line. 但是,当我尝试运行它时,在每个“添加”行上都出现“期望的标识符”错误。

You can initialize the ArrayList like this: 您可以像这样初始化ArrayList

ArrayList<String> stats = new ArrayList<>(Arrays.asList("STR", "DEX"));

as is shown in this answer . 如此答案所示。 Or, just put the add calls inside a method or constructor like this: 或者,只需将add调用放在这样的方法或构造函数中:

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

// Adding to ArrayList inside a constructor
public CharacterCreator()
{
    stats.add("STR");
    stats.add("DEX");
}

You cannot have bare statements like that in the class body. 您不能在类主体中具有裸露的语句。 You need to populate the array in a method, constructor or initialization block: 您需要在方法,构造函数或初始化块中填充数组:

public class CharacterCreator extends Application{
    ArrayList<String> stats;

    public CharacterCreator() {
         stats = new ArrayList<String>();
         stats.add("STR");
         stats.add("DEX");
         stats.add("CON");
         stats.add("INT");
         stats.add("WIS");
         stats.add("CHA");
    }
}

If you want the stats to be defined on the class level instead of the object level, you'll need to add a static initialization block: 如果要在类级别而不是对象级别上定义统计信息,则需要添加一个静态初始化块:

public class CharacterCreator extends Application{
    static ArrayList<String> stats;

    static {
         stats = new ArrayList<String>();
         stats.add("STR");
         stats.add("DEX");
         stats.add("CON");
         stats.add("INT");
         stats.add("WIS");
         stats.add("CHA");
    }
}

What about initializing block? 那初始化块呢?

public class CharacterCreator extends Application {
    public ArrayList<String> stats = new ArrayList<String>();
    {
       stats.add("STR");
       stats.add("DEX");
       stats.add("CON");
       stats.add("INT");
       stats.add("WIS");
       stats.add("CHA");
    }
}

as @shmosel just said you can't run statements outside a method. 就像@shmosel刚才说的那样,您不能在方法外部运行语句。 you can create an initializer method and call it in the constructor or move instructions into the constructor. 您可以创建一个初始化方法并在构造函数中调用它,或将指令移入构造函数。 add method is considered as unknown and so can't be resolved add方法被认为是未知的,因此无法解决

You can not call add() method while declaration of class. 声明类时不能调用add()方法。 Use static block or call method in constructor. 在构造函数中使用static块或调用方法。

public class CharacterCreator extends Application {

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

    public CharacterCreator() {
        stats.add("STR");
        stats.add("DEX");
        stats.add("CON");
        stats.add("INT");
        stats.add("WIS");
        stats.add("CHA");
    }

    public void start(Stage primaryStage) {

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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