我有代码可以接受用户输入并增加一个计数器以用于打印和删除目的。我添加了一些打印行来查看链表和当前位置之间的计数差异,这就是我得到的:
Enter a command from the list above (q to quit):
2
Deleted: e e 5 $5.0
Current record is now first record.
4
5
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
at java.util.LinkedList.entry(LinkedList.java:365)
at java.util.LinkedList.get(LinkedList.java:315)
at bankdata.command(bankdata.java:158)
at bankdata.main(bankdata.java:314)
Java Result: 1
BUILD SUCCESSFUL (total time: 18 seconds)
输入命令2,删除当前节点命令。当前节点是链表中的最后一个节点,其大小为 5,从技术上讲是 0-4。
那么当我运行这段代码时怎么会:
//currentAccount is a static int that was created at the start of my code.
//It got it's size because the int is saved every time a new node is made.
//The most recent size correlates with the last position in the linked list.
int altefucseyegiv = accountRecords.size();
System.out.println("Deleted: " + accountRecords.get(currentAccount)
+ "\n Current record is now first record.");
System.out.println(currentAccount);
System.out.println(accountRecords.size());
accountRecords.remove(currentAccount);
System.out.println("Deleted: " + accountRecords.get(currentAccount)
+ "\n Current record is now first record.");
if(altefucseyegiv == 1)
{
currentAccount = -1;
}
else
{
currentAccount = 0;
}
records.currentAcc(currentAccount, accountRecords);
return;
我得到这个错误???
我很困惑!因为我要删除 .get(4)th,这意味着我只是删除了第 5 个元素,我不是说爱。有人可以解释一下并可能帮我解决这个问题吗?