我在处理带有自定义对象的特定方法时遇到问题:
我创建了一个类"NodeQueue",其中包含节点列表(SList<"Node">(,它的长度和列表的第一个元素。
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"的实际实例中删除。
我应该如何更新节点队列并在单个函数中返回第一个元素?
我基本上需要将"Node a"设置为NodeQueue的第一个元素,并将"NodeQueue n"设置为自身减去第一个元素。
你.. 只是.. 做吧。
public class Example {
private int field;
public int changeAndReturnSomething(int in) {
this.field += in; //I changed a field...
return in * 2; // and I return something!
}
}
似乎微不足道。