use*_*636 2 java stack-overflow
尝试将项目添加到列表并打印它们,它编译,但我得到一个运行时错误,其中堆栈溢出错误.这是错误打印出来的:
Exception in thread "main" java.lang.StackOverflowError
at List.<init>(List.java:5)
at List.<init>(List.java:9)
at List.<init>(List.java:9) <----- this line is repeated quite a few times
Run Code Online (Sandbox Code Playgroud)
这是我的代码,包含添加和打印列表的方法.
public class List {
private AthleteNode front;
public List(){
front = null;
}
public List athletes = new List();
//add athlete to the end of the list
public void add(Athlete a){
AthleteNode node = new AthleteNode (a);
AthleteNode current; //temp node to iterate over the list
if(front == null)
front = node;//adds the first element
else{
current = front;
while (current.next !=null)
current = current.next;
current.next=node;
}
}
Run Code Online (Sandbox Code Playgroud)
在您的类中List,您有一个List在其声明中初始化的实例字段
public List athletes = new List();
Run Code Online (Sandbox Code Playgroud)
这意味着每次List都会有一个List具有List其中有一个List,令人作呕,造成StackOverflowError作为正在建设他们.
我不确定你对这个领域的意图,因为你没有在任何地方使用它.只需删除它.
| 归档时间: |
|
| 查看次数: |
791 次 |
| 最近记录: |