我正在尝试创建一个类,该类将名称的文本文件读入数组,然后将该数组返回给主类。但是,我在尝试定义数组时遇到错误。
public class Test{
String[] foo;
String[] zoo;
String[] yoo;
}
我在 String[] yoo 上收到错误
Syntax error on token ";", { expected after this
token
我真的不知道发生了什么,有人能帮忙吗?
编辑 - 代码的实际部分
String[] swords;
String[] prefix;
String[] suffix;
String[] rarity;
String[] colors = {"2","3","4","5","6","7","9","a","b","c","d","e","f"};
String[] bows = new String[3];
String[] enchantments = {"Frost","Igniton","Projection","Explosion","Enhance Jump","Enhance Speed","Resist Flames","Invisibility"};
rarity = new String[1000];
swords = new String[1000];
bows = new String[1000];
prefix = new String[1000];
suffix = new String[1000];
最佳答案
您不能将值分配给字段声明或 block (或构造函数)之外的字段。所以这一行
rarity = new String[1000];
(和其他类似的)应该在构造函数中,或者声明也应该初始化字段:
String[] rarity = new String[1000];
关于java - 在类中定义数组时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16873985/