2014-10-17 2 views
-1

내 자신의 이중 연결 목록을 만들어야합니다. 나는 초보자이기 때문에 내 지식 부족을 용서해주십시오. 이 목록은 List java 인터페이스를 구현해야하므로 remove (int), remove (Object) 및 clear() 메서드도 있으며 clear() 메서드는 작업을 수행하지 않습니다. 목록의 모든 요소를 ​​제거하고 일부만 제거하십시오. 여기 연결된 목록 .clear() 메서드가 제대로 작동하지 않습니다.

맑은() 방법 :

public void clear() { 
    for (T t : this) { 
     this.remove(t); 
    } 
    this.remove(this.size); 
} 

제거 (Object) 메소드 :

public boolean remove(Object o) { 
    if (this.indexOf(o) >= 0){ 
     remove(this.indexOf(o)); 
     return true; 
    } 
    return false; 
} 

그리고 마지막으로, 제거 (int) 메소드 :

public T remove(int index) { 
    if (getNode(index) == null || this.isEmpty()) { 
     return null; 
    } else if (this.size == 1){ 
     this.size = 0; 
     Node<T> currentNode = this.firstNode; 
     this.firstNode = null; 
     this.lastNode = null; 
     return currentNode.data; 
    } 

    Node<T> currentNode = this.getNode(index); 

    if (currentNode.nextNode != null){ 
     if (currentNode.previousNode != null){ 
      currentNode.previousNode.nextNode = currentNode.nextNode; 
     } else { 
      this.firstNode = currentNode.nextNode; 
      this.firstNode.previousNode = null; 
      this.size--; 
      return currentNode.data; 
     } 
    } 
    if (currentNode.previousNode != null){ 
     if (currentNode.nextNode != null) { 
      currentNode.nextNode.previousNode = currentNode.previousNode; 
     } else { 
      this.lastNode = currentNode.previousNode; 
      this.lastNode.nextNode = null; 
      this.size--; 
      return currentNode.data; 
     } 
    } 
    currentNode = currentNode.nextNode; 
    this.size--; 

    for(int i = index; i < this.size-1; i++){ 
     currentNode = currentNode.nextNode; 
     currentNode.index--; 
    } 

    return currentNode.data; 
} 

가능한 경우 버그의 위치를 ​​지적 할뿐만 아니라이 코드를 개선하는 데 도움을주십시오. 노력해 주셔서 감사합니다!

+0

'for (T t : this) { this.remove (t); }'는 열거자를 완전히 망치고 있습니다. @ njzk2가 언급 된대로 – njzk2

+0

대신에 for (int i = size() - 1; i> = 0; i--) {}'루프를 사용하면리스트를 반복하는 동안 코드가'ConcurrentModificationException'을 throw합니다. 당신이 항목을 제거하고 있습니다 – Branky

+0

@ njzk2 감사합니다. 비록 더 이상 문제가 보이더라도 이것을 포함한 답을 만드십시오. –

답변

4

clear() 메서드는 해당 요소가 반복되는 동안 List에서 요소를 제거한다는 점에서 매우 의심 스럽습니다. 내장 된 List 구현 중 하나를 사용하여 시도한 경우 ConcurrentModificationException이 표시됩니다. 실제로 실제로 그런 종류의 것을 처리 할 수있는 List을 구현하는 것은 상당히 어렵습니다.

어쨌든 나는 보통 remove(int) 메서드가 목록의 유일한 요소를 제거 할 때처럼 노드 목록에 대한 참조를 무효화하기 위해 Java 연결 목록의 clear() 메서드를 사용합니다. clear()은 목록의 내용에 관계없이 동일하게 처리 할 수 ​​있어야합니다. 추가

편집 : 당신이 사용할 수있는 것처럼

특히, 당신은 질문 때문에, 보이는

public void clear() { 
    this.firstNode = null; 
    this.lastNode = null; 
    this.size = 0; 
} 

(this.의 사용이 불필요, 나는 일반적으로 그것을 할 것이지만, 나는 당신의 다른 코드의 스타일을 따르고있다.) 내가 처음에는 특정 코드를 포함하지 않은 이유는 이것이 구현에 100 % 정확하고 충분하다는 것을 확신하기에 충분한 정보가 없다는 점이다.

+0

'clear() {while (! isEmpty()) {remove (0); }}' –

+1

@JoopEggen 그렇습니다. 역순으로 인덱스를 반복하는 것보다 낫지 만'clear() {firstNode = null; lastNode = null; 크기 = 0; }'. –

+0

감사합니다. 이것은 내 문제를 해결했다. 누구든지 좀 더 자세한 답변을 게시 할 수 있도록하기 위해 잠시 기다릴 것이지만, 그러면이를 받아 들일 것입니다. –

관련 문제