这是我的一部分代码。我有insertPoint方法的问题。我有一个我需要命名的数组。在insertPoint方法中,我给出了一个String变量,它给出了方法的名称。但是它不会抱着名字抱怨"表达式的类型必须是一个数组类型,但它解析为字符串"。我尝试添加"节点名称[]"但它给了我一个错误说"公开局部变量名"。不知道如何解决这个问题。
public class Node {
String name;
public void newArray(int number) {
this.name = "children" + number;
final Node name[] = new Node[4]; // Node's name should now be e.g. "children0"
insertPoint(this.x, this.y, size, this.name);
}
public void insertPoint(int x, int y, int size, String name) {
// Node name[]; // dublicate local variable name
if (this.x < c) {
if (name[0] == null) { // the type of the expression must be an array type but it resolved to string
name[0] = new Node(x, y, 0, 0, length);
} else {
newArray(0);
}
}
}
}
答案 0 :(得分:0)
Java
不支持动态变量声明或更改。在您的代码中,您尝试将节点名称命名为children0
。 Java编译器不会更改变量名称。
this.name = "children" + number;
final Node name[] = new Node[4];
您可以使用Map将动态变量存储为键和值。
Map mMap = new HashMap();
mMap.put("children0",your any value); //to insert
mMap.get("children0");//to get back