2016-12-12 2 views
0

iterator를 확장하고 두 개의 메서드를 추가하는 LLIterator라는 인터페이스를 만들었습니다.이 클래스를 사용하려고 할 때 컴파일 할 수 없었습니다. 여기에 내가 가진 것이있다. 내 사용자 인터페이스는 다음과 같이이다 :내 인터페이스에서 메서드를 찾을 수 없습니다.

 import java.util.Iterator; 

     public interface LLIterator<T> extends Iterator<T>{ 

     boolean hasNext(); 

     T next(); 

     void remove(); 

     void addBefore(T element); 

     void addAfter(T element); 

     } 

내 LinkedList의이 같은 수 있습니다 : 다른 클래스에서

import java.lang.StringBuilder; 
import java.util.NoSuchElementException; 
import java.util.Iterator; 
import java.util.ArrayList; 


/** 
* A class to represent a linked list of nodes. 
*/ 
public class LinkedList<T> implements Iterable<T>{ 
    /** the first node of the list, or null if the list is empty */ 
    private LLNode<T> first; 

/** some codes here 

@Override 
    public LLIterator<T> iterator() { 
    return new LinkedListIterator(); 
    } 

    private class LinkedListIterator implements LLIterator<T>{ 
    LLNode<T> nodeptr = first; 
    final LinkedList<T> list = getList(); 

    @Override 
    public void addBefore(T element){ 
     if(nodeptr == first || list.isEmpty()){ 
     throw new NoSuchElementException(); 
     } 
     LLNode<T> newnode = new LLNode<T>(element, null); 
     newnode.setNext(nodeptr); 
    } 

    @Override 
    public void addAfter(T element){ 
     LLNode<T> newnode; 
     if(nodeptr == first || list.isEmpty()){ 
     newnode = new LLNode<T>(element, null); 
     list.setFirst(newnode); 
     } 
     newnode = new LLNode<T>(element, null); 
     newnode.setNext(nodeptr.getNext()); 
     nodeptr.setNext(newnode); 
    } 
    } 

, 나는 그것을 컴파일하지만 때이 Linkedlist의 반복자 방법을 사용 addBefore 방법이라고 , 여기

cannot find symbol 
    symbol: method addAfter(WordData) 
    location: variable iterator of type java.util.Iterator<WordData> 

을 보여줍니다 내 다른 클래스의 일부입니다

public void addFollowingWord(String word){ 
    Iterator<WordData> iterator = list.iterator(); 
    WordData current = iterator.next(); 
    while(iterator.hasNext()){ 
     if(current.getWord().equals(word)){ 
     current.incrementCount(); 
     } 
     else if(current.getWord().compareTo(word) < 0){ 
     current = iterator.next(); 
     } 
     else if(current.getWord().compareTo(word) > 0){ 
     WordData newword = new WordData(word); 
     iterator.addBefore(newword); 
     } 
    } 
    if(!iterator.hasNext()){ 
    WordData newword = new WordData(word); 
     iterator.addAfter(newword); 
    } 
} 

매우 혼란 스럽습니다. 어딘가에서 실수를 했습니까?

+1

는 PLZ를 살펴 클래스 – firephil

+1

의 전체 코드를 게시 – Rio

답변

4

사용자 인터페이스가 Iterator으로 캐스트 되었기 때문에 사용자 인터페이스의 메소드에 액세스 할 수 없습니다. ,

@Override 
public LLIterator<T> iterator() { 
    return new LinkedListIterator(); 
} 
+0

내 질문에 대답하는 시간을 쳐 주셔서 대단히 감사합니다 대단히 감사합니다,하지만 난 그것을 수정 후 : 당신의 서브 인터페이스를 반환하기 위해 iterator() 방법 업데이트 당신이 말했듯이, 그것은 작동하지 않는 것 같아요 .... 내가 게시하는 새로운 코드를 살펴 보시기 바랍니다 수 있습니다. – Rio

+0

@Rio 당신은 여전히 ​​'Iterator' 인터페이스를 사용하고 있습니다. 'LLIterator '을 사용해야합니다. – shmosel

+0

sry man, 나는 여기에서 변화하지 않았지만, 나는 내 코드에서 변화가 있었지만 여전히 나에게 같은 오류를 준다. – Rio

관련 문제