gpt4 book ai didi

java - 为什么这没有意义?删除节点

转载 作者:行者123 更新时间:2023-12-01 07:10:17 25 4
gpt4 key购买 nike

我的代码可以接受用户输入并增加计数器以用于打印和删除目的。我添加了一些打印行来查看链接列表和当前位置之间的计数差异,这就是我得到的:

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) 个元素,这意味着我只是删除第五个元素,我并不是指爱。有人可以解释一下并可能帮助我解决这个问题吗?

最佳答案

线路抛出IOFB异常

System.out.println("Deleted: " + accountRecords.get(currentAccount)
+ "\n Current record is now first record.");

您已删除了第 5 个元素,因此现在没有可显示的第 5 个元素(记住数组位置从 0 开始)

关于java - 为什么这没有意义?删除节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15680981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com