String[] files= {};
int[] fileNumber = {0};
String commandPromptTxt = "";
String CPTDummy = "";
String blankDummy = "";
String[] currentFile = {};
void makeFile(String[] file, int fileNum, String name1, int level1, int[]parents1, int[] children1, String type1) {
//Warning if you make a file and use the same file number more than once you will override the file
files[fileNum]= {"10"};
};所以,在处理过程中,我有一段很棒的代码,我得到了一个错误unexpected token:{,我说files[fileNum] = {};,甚至当我在括号中输入值时,我也会得到相同的错误。有什么解决办法吗?谢谢。
发布于 2016-02-21 01:04:29
你为什么要把括号包括进去?
您使用的语法是数组初始化器。你在这里正确使用它:
String[] files= {};这会将files变量初始化为空数组。您还在这里正确地使用了语法:
int[] fileNumber = {0};这会将fileNumber变量初始化为具有单个索引的数组,在该索引中是值0。
这条线不再有意义了:
files[fileNum]= {"10"}首先,您已经将files变量初始化为具有零索引的数组。这意味着,即使这样编译,也会得到一个ArrayIndexOutOfBoundsException,因为您试图使用没有任何索引的数组的索引。
其次,您滥用了数组初始化语法。我很确定您不希望数组的索引也是数组,否则就必须使它们成为2D数组。
因此,总结一下,你需要做两件事:
1:初始化数组,使其具有实际的索引。就像这样:
String[] files = new String[10]; //array with 10 indexes2:停止滥用数组初始化语法,只向数组索引传递值:
files[fileNum]= "10";不过,使用ArraysLists可能会更好。然后,您不需要提前知道有多少索引,只需调用add()函数就可以向它们添加内容。
更多信息可以在加工参考中找到。
https://stackoverflow.com/questions/35530221
复制相似问题