错误:需要数组,但找到了字符串 [英] error: Array required, but String found
问题描述
我已经声明了名称和ID的公共静态数组:
I've declared public static arrays for name and id:
public static String[] name = new String[19];
public static int[] id = new int[19];
但是java compiler
说:
java:70: error: array required, but String found
java:71: error: array required, but int found
我不知道怎么了.是我声明变量的方式还是我编写的方法?
I don't know what's wrong. Is it how I declared the variables or in the method that I wrote?
public static boolean add(String name, int id, int i)
{
if (i < 20) {
name[i] = name;
id[i] = id;
return true;
}
else if (i > 20) {
for (int j = 0; j < id.length; j++) {
if (id[j] == 0 && name[j].equals("null"))
id[j] = id;
name[j] = name;
}
return true;
}
else
return false;
}
推荐答案
在静态name
String数组和传递给add
方法的本地name
String变量之间,您会发生冲突.
You have a collision between the static name
String array and the local name
String variable passed to the add
method.
最好的解决方案是使用不同的名称.这样会使代码更容易理解.
The best solution would be to use different names. It would make the code much easier to understand.
如果您仍然坚持使用相同的名称,则可以通过使用类名称访问静态数组来解决名称冲突:
If you still insist on using the same name, you can resolve the name collision by accessing the static array using the class name:
YourClassName.name[i]= name;
id
int数组和id
int变量也是如此.
The same applies to your id
int array and id
int variable.
这篇关于错误:需要数组,但找到了字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!