[英]array required, but java.lang.String found Java Class Array Error
我收到以下錯誤:
array required, but java.lang.String found
我不知道為什么。
我想做的是將一個對象的實例(我相信這是正確的術語)放入該類型的(對象的)類數組中。
我上課:
public class Player{
public Player(int i){
//somecodehere
}
}
然后在我的main方法中創建它的一個實例:
static final Player[] a = new Player[5]; // this is where I'm trying to create the array.
public static void main(String[] args){
Player p = new Player(1);
a[0] = p; //this is the line that throws the error
}
任何想法為什么會這樣?
在您的代碼中,我認為發生該錯誤的唯一方法是
static final Player[] a = new Player[5]; // this is where I'm trying to create the array.
public static void main(String[] args){
String a = "...";
Player p = new Player(1);
a[0] = p; //this is the line that throws the error
}
在這種情況下,您的局部變量a
將遮蓋同名的static
變量。 數組訪問表達式
a[0]
因此會導致編譯錯誤,例如
Foo.java:13: error: array required, but String found
a[0] = p; // this is the line that throws the error
因為a
不是數組,但是[]
表示法僅適用於數組類型。
您可能只需要保存並重新編譯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.