【发布时间】:2019-04-22 11:48:00
【问题描述】:
目前,我正在研究 Java 中的通用列表。问题:prepend 方法没有按应有的方式工作。它不是在索引 0 处添加元素 T,而是创建了一个无限递归列表。
public class Vector<T>{
private T value;
private Vector<T> next = null;
public Vector(T value){
this.value = value;
}
public Vector(T value, Vector<T> next){
this.value = value;
this.next = next;
}
public void prepend(T element){
this.next = this;
this.value = element;
}
}
public class Main{
...
Vector<Integer> v1 = new Vector<Integer>(new Integer(1));
v1.prepend(new Integer(0));
...
预期输出:{0,1} 实际输出:{0,0,0,0,0,0,0, ........ }
【问题讨论】:
标签: java list generics recursion generic-list