2014-09-08 3 views
1

Я создал Doubly Linked List, и я пытаюсь вызвать метод getData из связанного списка. Однако он не работает. Я пытаюсь получить это из узла. Вот код из узла.DoublyLinkedList получение данных с узла

private class Node<AnyType> 
{ 

    AnyType data; 
    Node<AnyType> next; 
    Node<AnyType> previous; 

    //Creates the Node with the parameters of data next and previous 
    public Node(AnyType data,Node<AnyType> next, Node<AnyType> previous) 
    { 

     this.data = data; 
     this.next = next; 
     this.previous = previous; 

    } 

    //Getters and setters for data next and previous 
    public AnyType getData() { 
     return data; 
    } 

    public Node<AnyType> getNext() { 
     return next; 
    } 

    public Node<AnyType> getPrevious() { 
     return previous; 
    } 

    public void setData(AnyType data) { 
     this.data = data; 
    } 

    public void setNext(Node<AnyType> next) { 
     this.next = next; 
    } 

    public void setPrevious(Node<AnyType> previous) { 
     this.previous = previous; 
    } 
} 

Это может быть проблемой, что он говорит, что УстановитьДанные (данные AnyType), никогда не используется локально, однако им не уверен в этом.

Теперь, где im пытается использовать метод getData. Это в методе одушевленным,

if (USE_LINKED_LIST) 
    { 
     for (int i = 0; i < this.linked_list.size(); i++) 
     { 
      Movable current = this.linked_list.getData(); 
      current.move(frame_rate_duration); 

      if(current.dead()) 
      { 
       this.linked_list.remove(current); 
       i--; 
      } 

     } 
    } 

this.linked_list.getData() дает мне ошибку говоря, что я должен создать метод GetData() в DoublyLinkedList. Я уверен, что это простая ошибка, но что-то помогает! благодаря!

Вот весь LinkedList класс

package Our_Fireworks; 

/** 
* 
* @author Ben Hammond 
* 
* @param <AnyType> 
*/ 


public class DoublyLinkedList <AnyType> 
{ 
private Node<AnyType> header; 
private Node<AnyType> footer; 

public DoublyLinkedList() 
{ 
    //Creates the header Node with data set to null, next set to footer, previous set to null 
    header = new Node<AnyType>(null, footer, null); 

    footer = new Node<AnyType>(null, null, header); 


} 

// Creates the insert method used to insert a Node into the linked list 
public void insert(AnyType data) 
{ 
    //Creates a new node to insert before the footer 
    Node<AnyType> newNode = new Node<AnyType>(data, footer, footer.previous); 
    //Sets the node previous to footer, to link to the new Node 
    footer.previous.setNext(newNode); 
    //Sets the footer node to be linked to the new Node 
    footer.setPrevious(newNode); 
} 

//Remove method to remove a Node from the linked list 
public void remove (AnyType data) 
{ 
    //Starts the iteratorLooper from the first Node in the list 
    Iterator<AnyType> iteratorLooper = first(); 

    //Runs the while loop as long as valid = true 
    while(iteratorLooper.valid()) 
    { 
     //Once you receive the correct data, the loop will stop 
     if(iteratorLooper.getData().equals(data)) 
     { 
      break; 
     } 
     //Goes to the next data member 
     iteratorLooper.next(); 
    } 

    //Once the while loop breaks, it will delete that data member 
    iteratorLooper.remove(); 

} 

//Creates the size method 
public int size() 
{ 
    //Creates an int variable 
    int count = 0; 
    //Starts the iteratorLooper at the first Node 
    Iterator<AnyType> iteratorLooper = first(); 

    //As long as valid returns true the while loop will run 
    while(iteratorLooper.valid()) 
    { 
     //Will add to the count variable 
     count++; 
     //Goes to the next Node 
     iteratorLooper.next(); 
    } 
    //Returns the count once the while loop is complete 
    return count; 

} 

//Creates the first method 
public Iterator<AnyType> first() 
{ 
    //Creates a new Iterator, at header.next 
    Iterator<AnyType> newIterator = new Iterator<AnyType>(header.next); 
    //Returns the Iterator 
    return newIterator; 
} 
//Creates the last method 
public Iterator<AnyType> last() 
{ 
    //Creates a new Iterator at footer.previous 
    Iterator<AnyType> newIterator = new Iterator<AnyType>(footer.previous); 
    //Returns the Iterator 
    return newIterator; 
} 

//Iterator find method 
public Iterator<AnyType> find(AnyType data) 
{ 

    Iterator<AnyType> iteratorLooper = first(); 
    //As long as valid returns true the while loop runs 
    while(iteratorLooper.valid()) 
    { 
     //runs the loop until data is equal to "getData" 
     if(iteratorLooper.getData().equals(data)) 
     { 
      break; 
     } 

     iteratorLooper.next(); 
    } 

    //Returns iteratorLooper 
    return iteratorLooper; 




} 

//Creates the Node class 
private class Node<AnyType> 
{ 

    AnyType data; 
    Node<AnyType> next; 
    Node<AnyType> previous; 

    //Creates the Node with the parameters of data next and previous 
    public Node(AnyType data,Node<AnyType> next, Node<AnyType> previous) 
    { 

     this.data = data; 
     this.next = next; 
     this.previous = previous; 

    } 

    //Getters and setters for data next and previous 
    public AnyType getData() { 
     return data; 
    } 

    public Node<AnyType> getNext() { 
     return next; 
    } 

    public Node<AnyType> getPrevious() { 
     return previous; 
    } 

    public void setData(AnyType data) { 
     this.data = data; 
    } 

    public void setNext(Node<AnyType> next) { 
     this.next = next; 
    } 

    public void setPrevious(Node<AnyType> previous) { 
     this.previous = previous; 
    } 
} 


//Creates the Iterator class 
public class Iterator<AnyType> 
{ 
    //Creates a new node of currentNode 
    private Node<AnyType> currentNode; 

    public Iterator(Node<AnyType> currentNode) 
    { 
     this.currentNode = currentNode; 
    } 

    //Creates the valid method 
    public boolean valid() 
    { 
     //Checks to see if current node is not equal to the header footer, or null 
     if (currentNode != header && currentNode != footer && currentNode != null) 
       { 
        //If the statement is true it returns true 
        return true; 

       } 
     else 
       { 
        //If it is not true... it simply returns false 
        return false; 
       } 

    } 
    //Creates the next method 
    public void next() 
    { 
     //Checks if the next Node is not equal to null 
     if(currentNode.getNext() != null) 
     { 
      //Gets the next node, of what ever the current node is 
     currentNode = currentNode.getNext();  
     } 
    } 
    //Creates the previous method 
    public void previous() 
    { 
     //Checks if the previous Node is not equal to null 
     if(currentNode.getPrevious() != null) 
     { 
      //Gets the previous node of currentNode 
      currentNode = currentNode.getPrevious(); 
     } 
    } 

    public AnyType getData() 
    { 
     //Gets the data inside the currentNode 
     return currentNode.getData(); 
    } 

    //Creates the remove method 
    public void remove() 
    { 
     //As long as valid returns true than the if statement will run 
     if(valid()) 
     { 
     currentNode.getPrevious().setNext(currentNode.getNext()); 

     currentNode.getNext().setPrevious(currentNode.getPrevious()); 
     currentNode = currentNode.getPrevious(); 
     } 


    } 

    //Creates the insert method with the parameters of AnyType and data 
    public void insert(AnyType data) 
    { 
     //Creates a newNode to be inserted after currentNode 
     Node<AnyType> newNode = new Node<AnyType>(data, currentNode.next, currentNode); 


     currentNode.getNext().setPrevious(newNode); 
     currentNode.setNext(newNode); 

    } 

    } 










    } 
+0

У вас есть класс под названием AnyType, или вы хотите, чтобы тип был чем-нибудь? – csmckelvey

+0

У меня нет класса под названием AnyType, я бы хотел, чтобы это было что угодно. –

+0

Отправьте код для вашего 'DoublyLinkedList', так как это кажется классом, который не имеет метода. – csmckelvey

ответ

1

Вы getData() метод Node, а не для связанного класса списка. Я думаю, что вы имели в виду что-то вроде

Movable current = this.linked_list.get(i).getData(); 

(при условии, связанный класс список содержит поглотитель для индекса)

Обычно связаны списки не имеют случайные методы получения доступа, так что, скорее всего, весь код должен быть написан по-разному :

for (Node<Movable> node = linked_list.getHead(); node != null; node = node.getNext()) { 
    Movable current = node.getData(); 
    ... 
} 

EDIT: Итак, у вас есть GetData() в вашем Iterator:

for (Iterator<Movable> iter = linked_list.first(); iter.valid(); iter.next()) { 
    Movable current = iter.getData(); 
    ... 
} 
+0

Если я это сделаю, я получаю ту же ошибку, она просто говорит, чтобы создать метод get (int) в DoublyLinkedList –

+1

@BenHammond, потому что нет, проверьте мой ответ. И вы должны предоставить код для 'DoublyLinkedList', если вам нужен точный ответ. –

+0

Я добавил весь DoyblyLinkedList, если вы хотите проверить это и увидеть, где ошибка может быть –

Смежные вопросы