我遇到了问题。代码:
// 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);
}
}
Main 函数如下所示:
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 个。这有什么问题?也许我不正确理解队列大小()?
添加打印方法:
public void displayActions()
{
for(Action s : actionsList)
{
System.out.println(s);
}
}