作者:百度地震姜常宏 | 来源:互联网 | 2023-12-14 17:38
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 个解决方案
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)。