[英]My ArrayList won't recognize what I add to the list
這是代碼本身
import java.util.ArrayList;
public class Student {
private String name;
private int age;
public Student (String n, int a) {
name = n;
age = a;
}
public String toString() {
return name + " is " + age + " years old";
}
ArrayList<Student> rayList = new ArrayList<Student>();
rayList.add(new Student("Sam", 17));
rayList.add(new Student("Sandra", 18));
rayList.add(new Student("Billy", 16));
rayList.add(new Student("Greg", 17));
rayList.add(new Student("Jill", 18));
public static void main(String[] args) {
System.out.println(rayList.get(0));
}
}
main方法中缺少某些println命令。 但是,當我嘗試將5個學生添加到ArrayList時,出現錯誤“無法對非靜態字段rayList進行靜態引用”
您正在嘗試在可執行上下文之外執行代碼。 只能從方法,靜態初始化程序或實例初始化程序(感謝NickC)上下文中執行代碼。
嘗試將其移入main
方法以開始...
public static void main(String[] args) {
ArrayList<Student> rayList = new ArrayList<Student>();
rayList.add(new Student("Sam", 17));
rayList.add(new Student("Sandra", 18));
rayList.add(new Student("Billy", 16));
rayList.add(new Student("Greg", 17));
rayList.add(new Student("Jill", 18));
System.out.println(rayList.get(0));
}
根據反饋進行了更新
您的第一個錯誤Cannot make a static reference to the non-static field rayList
生成Cannot make a static reference to the non-static field rayList
因為rayList
沒有聲明為static
,但是您試圖從static
上下文中引用它。
// Not static
ArrayList<Student> rayList = new ArrayList<Student>();
// Is static
public static void main(String[] args) {
// Can not be resolved.
System.out.println(rayList.get(0));
}
rayList
被聲明為“實例”字段/變量,這意味着它需要聲明類( Student
)的實例才有意義。
這可以通過...解決
在main
創建一個Student
實例,然后通過該實例訪問它,例如...
public static void main(String[] args) {
Student student = new Student(...);
//...
System.out.println(student.rayList.get(0));
}
我個人不喜歡這樣, rayList
並不真正屬於Student
,它沒有任何價值。 您能想象在將任何Student
添加到List
之前必須創建Student
實例嗎?
我也不喜歡直接訪問實例字段,但這是個人喜好。
使rayList
static
static ArrayList<Student> rayList = new ArrayList<Student>();
// Is static
public static void main(String[] args) {
//...
System.out.println(rayList.get(0));
}
這是一個可行的選擇,但是在被認為是好是壞之前,需要更多的上下文。 我個人認為, static
字段可能會導致更多問題,而不是解決的問題,但這再次是個人觀點,您的上下文可能會認為是合理的解決方案。
或者,您可以在static
方法的上下文中創建List
的本地實例,如第一個示例所示。
public static void main(String[] args) {
ArrayList<Student> rayList = new ArrayList<Student>();
//...
System.out.println(rayList.get(0));
}
就像我說的那樣,您選擇要做的一個取決於您,我個人更喜歡后兩個,但這就是我。
這是因為您正在訪問static function
內部的non static field
。 就你而言。
ArrayList<Student> rayList = new ArrayList<Student>(); // declare instance variable.
而且,您無法訪問沒有創建類實例的實例變量。
改用
public static void main(String[] args) {
ArrayList<Student> rayList = new ArrayList<Student>(); // creates local instance of rayList
rayList.add(new Student("Sam", 17));
rayList.add(new Student("Sandra", 18));
rayList.add(new Student("Billy", 16));
rayList.add(new Student("Greg", 17));
rayList.add(new Student("Jill", 18));
System.out.println(rayList.get(0));
}
該錯誤意味着rayList
不是靜態的(換句話說,它是Student
類的成員),但是您的main
方法是:
/** THIS IS NOT STATIC **/
ArrayList<Student> rayList = new ArrayList<Student>();
...
public static void main(String[] args) {
/** THIS SCOPE IS STATIC **/
System.out.println(rayList.get(0));
}
實際上,你不能讓列表Student
S作為的成員, Student
,因為你要么得到一個堆棧溢出或耗盡內存,這取決於你如何創建它,所以我猜你想rayList
是靜態的。 那將使rayList
不再是Student
的成員。
您也不能以您在初始化塊或方法之外的方式添加到ArrayList
。
有關初始化程序塊的更多信息,請參見此處:
或者,您可以將引用rayList
所有內容rayList
main
方法中。
將rayList
變量設置為static可以在main
方法中使用它,如下所示:
然后在main方法中添加對象並使用它:
static ArrayList<Student> rayList = new ArrayList<Student>();
public static void main(String[] arg){
rayList.add(new Student("Sam", 17));
rayList.add(new Student("Sandra", 18));
rayList.add(new Student("Billy", 16));
rayList.add(new Student("Greg", 17));
rayList.add(new Student("Jill", 18));
//Do whatever you want.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.