使用处理并在我的阵列上获取意外标记。 #黑客马拉松
Using processing and getting an unexpected token on my array. #hackathon
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] = {};
,即使我在括号中输入值,我也会得到同样的错误。有什么解决办法吗?谢谢
你为什么要包括括号?
您使用的语法是数组初始值设定项。你在这里正确使用它:
String[] files= {};
这会将您的 files
变量初始化为一个空数组。您在这里也正确使用了语法:
int[] fileNumber = {0};
这会将您的 fileNumber
变量初始化为具有单个索引的数组,该索引中的值是 0
.
这一行不再有意义:
files[fileNum]= {"10"}
首先,您已经将 files
变量初始化为一个 具有零索引 的数组。这意味着即使这可以编译,你也会得到一个 ArrayIndexOutOfBoundsException
,因为你正在尝试使用一个没有索引的数组。
其次,您误用了数组初始化语法。我很确定您不希望数组的索引 也是数组 ,否则您必须将它们设为二维数组。
所以,总结起来,你需要做两件事:
1: 初始化您的数组以实际拥有索引。像这样:
String[] files = new String[10]; //array with 10 indexes
2: 停止滥用数组初始化语法,只需将值传递到数组索引即可:
files[fileNum]= "10";
不过,您最好还是使用 ArraysLists
。然后你不需要提前知道你有多少个索引,你可以简单地调用 add()
函数来添加东西。
可以在 the Processing reference 中找到更多信息。
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] = {};
,即使我在括号中输入值,我也会得到同样的错误。有什么解决办法吗?谢谢
你为什么要包括括号?
您使用的语法是数组初始值设定项。你在这里正确使用它:
String[] files= {};
这会将您的 files
变量初始化为一个空数组。您在这里也正确使用了语法:
int[] fileNumber = {0};
这会将您的 fileNumber
变量初始化为具有单个索引的数组,该索引中的值是 0
.
这一行不再有意义:
files[fileNum]= {"10"}
首先,您已经将 files
变量初始化为一个 具有零索引 的数组。这意味着即使这可以编译,你也会得到一个 ArrayIndexOutOfBoundsException
,因为你正在尝试使用一个没有索引的数组。
其次,您误用了数组初始化语法。我很确定您不希望数组的索引 也是数组 ,否则您必须将它们设为二维数组。
所以,总结起来,你需要做两件事:
1: 初始化您的数组以实际拥有索引。像这样:
String[] files = new String[10]; //array with 10 indexes
2: 停止滥用数组初始化语法,只需将值传递到数组索引即可:
files[fileNum]= "10";
不过,您最好还是使用 ArraysLists
。然后你不需要提前知道你有多少个索引,你可以简单地调用 add()
函数来添加东西。
可以在 the Processing reference 中找到更多信息。