简体   繁体   English

为什么这没有道理? 删除节点

[英]Why doesn't this make sense? Removing Nodes

I have code that takes user input and increments a counter for printing and deleting purposes. 我有接受用户输入并为打印和删除目的增加一个计数器的代码。 I added some print lines to see what the count difference was between the linked list and the current position and this is what I got: 我添加了一些打印行,以查看链表和当前位置之间的计数差异,这就是我得到的结果:

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)

enter command 2, the remove current node command. 输入命令2,除去当前节点命令。 The current node is the last in the linked list which is 5 in size which technically means from 0-4. 当前节点是链表中的最后一个节点,其大小为5,从技术上讲,它的意思是0-4。

SO how come when I run this code: 当我运行这段代码时,结果如何:

//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;

I get this error??? 我收到此错误???

I'm confused! 我糊涂了! Because I'm removing the .get(4)th and that means I'm just removing the 5th element and I don't mean love. 因为我删除了.get(4),这意味着我只是删除了第5个元素,而我并不是说爱。 Can someone explain and possibly help me fix this please? 有人可以解释并可能帮助我解决此问题吗?

The IOFB Exception is thrown by the line 该行引发IOFB异常

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

You have removed the 5th element, so now there is no 5th element to show (remember array positions start on 0) 您已删除了第5个元素,所以现在没有要显示的第5个元素(记住数组位置从0开始)

Try 尝试

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

I'm assuming you have initialized currentAccount as accountRecords.size() - 1 and accountRecords has 5 nodes. 我假设你已经初始化currentAccountaccountRecords.size() - 1accountRecords有5个节点。

Then currentAccount has a value 4 and you are removing 4th element from the list leaving accountRecords with only 4 elements. 然后, currentAccount的值为4,您将从列表中删除第4个元素,而accountRecords仅包含4个元素。

Then you are trying to fetch accountRecords.get(4) from the list where accountRecords has only 4 elements and the valid element indexes are 0..3 , that is why you are getting the error. 然后,您尝试从accountRecords仅包含4个元素且有效元素索引为0..3的列表中获取accountRecords.get(4) ,这就是您得到此错误的原因。

i think your mistake is your println: 我认为您的错误是您的println:

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

<- you get the IndexOutOfBoundsException because you deleted the last entry of the list, or i am wrong? <-您收到IndexOutOfBoundsException,因为您删除了列表的最后一个条目,否则我错了? maybe try: 也许尝试:
System.out.println("Deleted: " + accountRecords.get(currentAccount-1) + "\\n Current record is now first record."); System.out.println(“已删除:” + accountRecords.get(currentAccount-1)+“ \\ n当前记录现在是第一条记录。”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM