つまり、私はDancerオブジェクトのラインを持っています。互いに接続されたオブジェクトの行末からオブジェクトを削除するにはどうすればよいですか? (Java)
public class Dancer {
private String name;
private Dancer next;
public Dancer(String nameInput, Dancer followingDancer){
name = nameInput;
next = followingDancer;
}
私はセッターとゲッターを持っています。
これらの文字列を文字列にするには、私はCongaLineを持っています。
public class CongaLine {
private Dancer head; // first dancer in the conga line.
public CongaLine() {
head = null;
}
だから、最後のダンサーの隣を見つけるために、whileループを使用して、どのように私は、抽出物にCongaLineから最後のダンサーを見つけるだろうか?欠陥がある
私の現在の方法は、次のようになります。
public String removeFromEnd() {
String removed = null;
// For multiple dancers, find the penultimate and remove its "next"
while (head.getNext() != null) {
if (head.getNext().getNext() == null){
removed = head.getNext().getName();
head.setNext(null);
}
}
// In the case of only one dancer, remove that dancer.
if (head != null && head.getNext() == null) {
removed = head.getName();
head = null;
}
return removed;
}
CongaLineでは、[LinkedList]タイプのフィールドがないのはなぜですか(http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html) ?プライベートリストのようなものダンサー=新しいLinkedList ();たとえば、dancer.getLast()を取得できます。 –
Jasonw
それはあまりにも簡単になります。私はあらかじめ定義されたクラスを変更することなくそれを行う必要があります。 – Jazzertron