java - 在类中定义数组时出错

标签 java arrays class object

我正在尝试创建一个类,该类将名称的文本文件读入数组,然后将该数组返回给主类。但是,我在尝试定义数组时遇到错误。

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/

相关文章:

php - 为数组中的每个项目循环 MySQL-Query 10 次(PHP 和 MySQL)

oop - 为什么 protected 和私有(private)属性可以由同一个类而不是同一个对象访问?

java - 如何在 Thymeleaf 中显示 BufferedImage

java - 未经授权的用户访问某些网址

java - php 中的 Jpos 替代方案

c++ - 使用指向二维数组行的指针来初始化其值。为什么这行得通?

JAVA绘制正多边形

python - 具有 3 个参数的 Numpy ndarray 形状

java - 为什么这个静态嵌套类不能在一台计算机上的 Java 中运行?

python - 将装饰器函数合并为类