我遇到问题。代码:队列有什么问题?
// withdraw method
public void withdraw(long n)
{
this.n = n;
Action a = new WithDraw();
a.doAction(n);
**if(actionsList.size() > 10)**
{
actionsList.poll();
actionsList.offer(a);
} else
{
actionsList.offer(a);
}
}
// Deposit method goes here
public void deposit(long n)
{
this.n = n;
Action a = new Deposit();
a.doAction(n);
**if(actionsList.size()<10)**
{
actionsList.offer(a);
} else
{
actionsList.poll();
actionsList.offer(a);
}
}
的主要功能如下:
acc1.deposit(1);
acc1.withdraw(2);
acc1.deposit(3);
acc1.withdraw(4);
acc1.deposit(5);
acc1.withdraw(6);
acc1.deposit(7);
acc1.withdraw(8);
acc1.deposit(9);
acc1.withdraw(10);
acc1.deposit(11);
acc1.withdraw(12);
acc1.deposit(13);
acc1.withdraw(14);
acc1.deposit(15);
acc1.displayActions();
我需要10最后添加的元素。在这之后,我得到了11个元素而不是10个。这是什么问题?也许我不明白Queue size()是否正确?
ADDED打印方法:
public void displayActions()
{
for(Action s : actionsList)
{
System.out.println(s);
}
}
请发表您的'displayActions'方法。 –
这就是我所理解的(这是否正确?):您有两种行为:撤回和存款。这些行为从银行账户中提取或存入资金等某些地方。所有这些行为都会排成一队,只有最后十个人应该呆在那里 - 无论是退出还是存款。我第一个不明白的是不对称的代码。队列方面,他们也是这样做的,应该有相同的代码 - 或者我误解了?你能解释一下吗? – jboi
首先,你理解正确。其次,这是否是对称的并不重要,因为我固定它使其对称,但结果是相同的。 – Ernusc