2016-06-27 6 views
0

割り当ての一部として、ジェネリックを実装しながら、ダミーヘッドノードを持つ単一リンク、非循環LinkedListを作成しています。この代入にはListインターフェイスの実装が必要ですが、私はsubListメソッドに固執しています。私はStackOverflowだけでなく、一般的にどのように私は自分のデザインのいくつかの異なるメソッドを試してみましたが、元のLinkedListに反映されていないサブリストの変更の例を見てしようとしている。私は(限り、私は任意のヘルパーメソッドを望んでいない)トップの答えhereの構造をしてみてくださいとフォローする私の方法を書き直し、そしてここに私の結果のコードです:ここではLinkedListのサブリストメソッドの作成

@Override 
public List<E> subList(final int fromIndex, final int toIndex){//FIX ME 
    if(fromIndex < 0 || fromIndex > this.size()-1 || toIndex < 0 || toIndex > this.size()-1){ 
    throw new IndexOutOfBoundsException("Index out of bounds on call to subList with fromIndex of" 
    + fromIndex + " and toIndex of" + toIndex); 
    } 
    List<E> list = new LinkedList<E>(); 
    Node<E> cur = this.head.next; 
    int count = 0; 
    while(cur!=null){ 
    if(count >= fromIndex && count < toIndex){ 
     list.add(cur.data); 
    } 
    cur = cur.next; 
    count++; 
    } 

    return list; 
}// end sublist 

は、私のテスターの抜粋ですファイル、あなたは私が正しいノードを持つサブリスト作成見ることができるように、しかしのsubListで行われた変更は、元のLinkedListに反映していない、と私はそれを修正するために続行するかどうかはわかりませんよ:

New LinkedList has been created 
List before testing: [one, two, three, four, five] 
Testing subList function with fromIndex of 1, and toIndex of 4 
Printing subList: [two, three, four] 
Changing data of sublist to 'six, seven, eight' 
Printing subList: [six, seven, eight] 
Printing LinkedList after test: [one, two, three, four, five] 

私は私のsubListが正しい選択であり、アドバイスや批判が大きく評価されるので、LinkedListを使用しているかどうかわからない!

編集:以下自分の質問に答え、私は基本的に新しいノードを作成するのではなく、元のLinkedList

答えて

0

内のノードを直接指していた私は、これは誰のために有用である期待して、自分の質問にお答えします将来これを見ようとしています。

私のコードでは、LinkedListでadd(int index、E data)関数を使用していましたが、データ変数自体を変更するのではなく、新しいNodeを作成してリストに挿入しました。それはもはや新しいノードを作成したので、だから私はそれを書き直していない、と私はこれに私の上記サブリストクラスを変更:

public List<E> subList(final int fromIndex, final int toIndex){//FIX ME 
    if(fromIndex < 0 || fromIndex > this.size()-1 || toIndex < 0 || toIndex > this.size()-1){ 
    throw new IndexOutOfBoundsException("Index out of bounds on call to subList with fromIndex of" + fromIndex + " and toIndex of" + toIndex); 
    } 
    LinkedList<E> list = new LinkedList<E>(); 
    Node<E> cur = this.head.next; 
    Node<E> pointer = list.head; 
    int count = 0; 
    while(cur!=null){ 
    if(count >= fromIndex && count < toIndex){ 
     pointer.next = cur; 
     list.size++; 
     pointer = pointer.next; 
    } 
    cur = cur.next; 
    count++; 
    } 
    return list; 

のではなく、私が直接にノードに私の元のLinkedList内のノードを割り当てられた追加機能を使用して私のsubList、および手動でインクリメントされたサイズ。

残念ながら、各ノードに次の参照があったため、手動で正しく追加したにもかかわらず、toString関数が呼び出されたときに、次のノードまで 'toIndex'を超えてノードを含むLinkedListを繰り返しましたヌルでした。この問題を解決するために、私はサイズを含め、私のtoStringで追加条件を追加し、また、返される文字列にカンマを追加するための私のif文を変更:

public String toString(){ 
    String ret = "["; 
    Node cur= this.head.next; 
    int index = 0; 
    while(cur != null && index < size){// added the index < size condition 
    ret = ret + cur.data; 
    if(index < this.size -1){// changed from cur.next != null 
     ret = ret + ", "; 
    } 
    cur = cur.next; 
    index++; 
    } 

    ret = ret + "]"; 
    return ret; 

}// end toString 

だから最終的には、私のテストの出力は次のようになります。

Testing subList function with fromIndex of 1, and toIndex of 4 
Printing subList: [two, three, four] 
Changing data of sublist to 'six, seven, eight' 
Printing subList: [six, seven, eight] 
Printing LinkedList after test: [one, six, seven, eight, five] 

うまくいけば、この応答は、将来的にこれと同じ問題に他人を助けることができる、私はまだ学んでいますし、私のコードはおそらく理想的ではありませんが!