【发布时间】:2020-10-08 20:44:56
【问题描述】:
我在使用自定义对象处理特定方法时遇到问题:
我创建了一个类“NodeQueue”,其中包含一个节点列表 (SList)、它的长度和列表的第一个元素
private final SList<Node> q;
private final int length;
private final Node first;
public NodeQueue() //Empty queue
{
q = new SList<Node>();
length = 0;
first = null;
}
public NodeQueue(SList<Node> q1) //Full queue
{
q = q1;
length = q.length(); //length() is the method that returns the length of SList<T>
first = q.car(); //car() is the method that returns the first element of SList<T>
}
public Node poll()
{
...
return first;
}
其中 poll() 的预期函数如下:
NodeQueue n = new NodeQueue(); //Let's pretend n is not empty
Node a = n.poll(); //Where "a" is the first element of the list
问题在于,在“poll()”中,我需要返回第一个元素(在本例中为节点 a)并将其从“NodeQueue n”的实际实例中删除。
我应该如何更新 Nodequeue 并在单个函数中返回第一个元素?
我基本上需要将“Node a”设置为NodeQueue的第一个元素,并将“NodeQueue n”设置为自身减去第一个元素。
【问题讨论】:
标签: java object methods instance