나는 문제에 직면했다. 코드 : 주요 기능은 다음과 같습니다대기열에 어떤 문제가 있습니까?
// 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 필요는 마지막 요소를 추가했다. 그 후에 나는 10이 아닌 11 가지 요소를 인쇄했습니다. 그게 뭐가 잘못 되었습니까? 큐 크기()를 올바르게 이해하지 못했을 수 있습니까?
ADDED 인쇄 방법 :
public void displayActions()
{
for(Action s : actionsList)
{
System.out.println(s);
}
}
'displayActions' 메소드를 게시하십시오. –
이것은 내가 알고있는 것입니다 (정확합니까?) : 두 가지 종류의 행동이 있습니다 : 철회와 예금. 이 조치는 은행 계좌로 /에서 돈을 어딘가에서 인출하거나 입금합니다. 이러한 모든 조치는 대기열에 들어가며 마지막 10 개는 인출 또는 예금과 상관없이 거기에 있어야합니다. 내가 처음 이해하지 못하는 것은 비대칭 코드입니다. 큐 단위로 동일한 코드를 사용해야합니다. 그렇지 않으면 오해 할 수 있습니까? 설명해 주시겠습니까? – jboi
먼저, 올바르게 이해합니다. 둘째, 대칭인지 아닌지는 중요하지 않습니다. 왜냐하면 대칭으로 만들었지 만 그 결과는 같습니다. – Ernusc