할당의 일부로 제네릭을 구현하면서 더미 헤드 노드와 함께 단일 링크 된 비 원형 LinkedList를 작성하고 있습니다. 이 할당에는 List 인터페이스 구현이 필요하지만 subList 메소드가 필요합니다. 나는 StackOverflow뿐만 아니라 일반적으로 내 자신의 디자인에 대한 여러 가지 다른 방법을 시도 했으므로 어떻게 완료되는지 예제를 보려고 노력하면서 웹을 수색했지만 subList의 변경 사항은 원래의 LinkedList에 반영되지 않습니다. 나는 (내가 어떤 헬퍼 메소드를하지 않을 정도로) 시도하고 here 최고 대답의 구조에 따라 내 방법을 재 작성하고 여기에 내 결과 코드 : 여기 LinkedList에 대한 subList 메서드 작성
@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
내 테스터의 조각입니다 파일, 당신은 그러나 원래 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]
I을 내 하위 목록으로 LinkedList를 사용하여 확실하지, 올바른 선택이며, 조언이나 비판 크게 받아 들여질 것입니다!
편집 : 나는 기본적으로 새로운 노드를 만들기보다는 원래 LinkedList의에 노드에서 직접 가리키고, 아래에있는 내 자신의 질문에 대답