[英]Method variable needs to be String but requires array
這是我的一部分代碼。 我的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);
}
}
}
}
Java
不支持動態變量聲明或更改。 在您的代碼中,您試圖將Node name命名為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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.