热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra

I am trying to make a class that will read a text file of names into an array, then return that array to the main class. However I am having an error when attempting to define the arrays.

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

public class Test{
String[] foo;
String[] zoo;
String[] yoo;
}

I get an error on String[] yoo

我在String [] yoo上收到错误

Syntax error on token ";", { expected after this 
token

I really have no clue what is going on, can anyone help?

我真的不知道发生了什么,有人可以帮忙吗?

Edit - Actual section of code

编辑 - 代码的实际部分

    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];

4 个解决方案

#1


1  

You should not initialize like this in outside the constructors or methods

您不应该在构造函数或方法之外进行这样的初始化

Wrong:

public Test{
 String[] rarity;
 String[] swords;
 rarity = new String[1000]; 
 swords = new String[1000];
}

You can do this

你可以这样做

public Test{
      String[] rarity = new String[1000]; 
      String[] swords = new String[1000];
    }

if the variables are static you can use static block

如果变量是静态的,则可以使用静态块

public Test{
   private static int x;
   static{
          x=2;
   }

}

Use constructor instead to initialize:

使用构造函数来初始化:

 public Test{
    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"};
  public Test(){
    rarity = new String[1000];
    swords = new String[1000];
    bows = new String[1000];
    prefix = new String[1000];
    suffix = new String[1000];
  }
}

That's all

#2


2  

You can't assign values to fields outside of the field declaration or a block (or constructor). So this line

您不能将值分配给字段声明或块(或构造函数)之外的字段。所以这一行

rarity = new String[1000];

(and the other similar ones) should be in the constructor, or the declaration should also initialize the field:

(以及其他类似的)应该在构造函数中,或者声明也应该初始化字段:

String[] rarity = new String[1000];

#3


1  

unless you post all of your code it is not possible to be sure the answer is correct.

除非您发布所有代码,否则无法确定答案是否正确。

but I guess you have this:

但我想你有这个:

rarity = new String[1000];
swords = new String[1000];
bows = new String[1000];
prefix = new String[1000];
suffix = new String[1000];

outside a method. that is not possible in Java.

在方法之外。这在Java中是不可能的。

do like this instead:

改为:

String[] rarity = new String[1000];

or init the field inside a method/constructor

或者在方法/构造函数中初始化字段

#4


0  

First of all, you should make them public or private(unless you really need it to be package-private).

首先,你应该将它们公开或私有(除非你真的需要它是包私有的)。

An array is created like this: Type[] variableName = new Type[length];

像这样创建一个数组:Type [] variableName = new Type [length];

length is the size of the array, for example String[] test = new String[5] can contain 5 strings. To set them use test[i] = someString; where i is the index(starting at 0 and ending at length - 1).

length是数组的大小,例如String [] test = new String [5]可以包含5个字符串。设置它们使用test [i] = someString;其中i是索引(从0开始,以长度结束 - 1)。

You can also make an ArrayList if you do not want your array to be limited, but that uses a bit more memory.

如果您不希望限制数组,但是使用更多内存,也可以创建一个ArrayList。

ArrayList variableName = new ArrayList<>();

ArrayList variableName = new ArrayList <>();

For example: ArrayList test = new ArrayList<>();

例如:ArrayList test = new ArrayList <>();

To add to it use test.add(someString) and to get: arrayList.get(i) where i is the index.

要添加到它使用test.add(someString)并获取:arrayList.get(i)其中i是索引。

A disadvantage of ArrayList is that primitive types(int, byte, boolean, ...) cannot be used. You'll need to use Integer, Byte, Boolean, ...

ArrayList的一个缺点是不能使用原始类型(int,byte,boolean,...)。你需要使用Integer,Byte,Boolean,...

If you have an ArrayList, you could intArrayList.add(5) because autoboxing transforms 5 into new Integer(5).

如果您有一个ArrayList ,则可以使用intArrayList.add(5),因为autoboxing将5转换为新的Integer(5)。


推荐阅读
author-avatar
百度地震姜常宏
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有