割り当ての一部として、ジェネリックを実装しながら、ダミーヘッドノードを持つ単一リンク、非循環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