【发布时间】:2018-02-23 20:23:01
【问题描述】:
你好,这是我没有实现 java.util.linkedlist 的链表
我想创建一个递归显示链表中所有元素的方法,但我不知道怎么做,我的方法没有任何参数,所以我不知道如何进入下一个调用方法本身时的值
public class Pokemon {
private String name;
private String type;
private int niveau;
public Pokemon(String name, String type) {
this.name = name;
this.type = type;
this.niveau = (int) (Math.random() * (1 * 1 - 100) + 100);
}
public void display() {
System.out.println(this.name);
}
public class Trainer {
public final String name;
private Pokeball head;
public Trainer(String name) {
this.name = name;
}
public void addPokemon(Pokemon pok) {
if (this.head != null) {
this.head.addPokemon(pok);
} else {
this.head = new Pokeball(pok);
}
}
public void display() {
if (this.head == null)
return;
else {
this.head.display();
}
}
public class Pokeball {
private Pokemon pok;
private Pokeball next;
public Pokeball(Pokemon pok) {
this.pok = pok;
}
public Pokeball(Pokemon pok, Pokeball next) {
this.pok = pok;
this.next = next;
}
public void addPokemon(Pokemon pok) {
Pokeball current = this;
while (current.next != null) {
current = current.next;
}
current.next = new Pokeball(pok);
}
public void display() {
Pokeball current = this;
if (current.next == null){
return;
} else {
// ....
}
}
【问题讨论】:
-
正确的缩进对于我们能够轻松阅读和理解您的代码至关重要。请修复它。
标签: java recursion linked-list