我创建了一个双向链接列表,我试图从链接列表中调用getData方法。但它不起作用。我试图从节点得到这个。以下是Node的代码。
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;
}
}
它可能是一个问题,它说setData(AnyType数据)从不在本地使用,但我不确定。
现在我试图使用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()给出了错误说我必须在DoublyLinkedList中创建方法getData()。我确定这只是一个简单的错误,但任何事情都有帮助!谢谢!
这是整个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 :(得分:1)
getData()
有Node
方法,而不是您的链接列表类。我认为你的意思是
Movable current = this.linked_list.get(i).getData();
(假设你的链表类有一个索引的getter)
通常链接列表没有随机访问获取者,因此很可能整个代码的编写方式不同:
for (Node<Movable> node = linked_list.getHead(); node != null; node = node.getNext()) {
Movable current = node.getData();
...
}
编辑:所以你在Iterator
:
for (Iterator<Movable> iter = linked_list.first(); iter.valid(); iter.next()) {
Movable current = iter.getData();
...
}