简体   繁体   English

如何修复此索引超出范围错误

[英]How do I fix this index out of bound error

Main Class主Class

        FileDealer.modify(t2, 0, "99999");
        FileDealer.modify(t2, 0, "88888");

FileDealer Class (Method: Modify) FileDealer Class(方法:修改)

public static void modify(Ticket t, int type, String newInformation) throws MacAddressLengthException, IOException {
        int index = ticketList.indexOf(t);
        Ticket toModify = null;
        toModify = ticketList.get(index);
        switch (type) {
        case 0: // adding mainID
            toModify.setMainID(Utility.convertInt(newInformation));
            break;
        case 1: // adding assetID
            toModify.setAssetID(Utility.convertInt(newInformation));
            break;
        case 2: // adding macAdd
            toModify.setMacAdd(newInformation);
            break;
        case 3: // edit
            toModify.setNote(newInformation);
            break;
        case 4: // delete
            ticketList.remove(index);
        }
        updateFile();
        readFile();
    }

when the modify method is called the first time it is able to edit the ticket details, but when it is called the 2nd time, the int index = ticketList.indexOf(t) from the method returns a "-1" even though nothing changed, and it gives me an exception for the index being out of bound.当第一次调用 modify 方法时,它可以编辑工单详细信息,但是当第二次调用时,该方法中的int index = ticketList.indexOf(t)返回“-1”,即使没有任何更改,它给了我一个索引超出范围的例外。 How do I change that -1 to show the right index?如何更改 -1 以显示正确的索引?

Probably the equals method of the Ticket class depends in any way of the mainID field, and it seems that you modify that field in the modify method.可能Ticket class 的equals方法取决于mainID字段的任何方式,并且您似乎在modify方法中修改了该字段。

As a consequence, the second time you invoke modify , indexOf returns -1 because equals evaluates to false .因此,第二次调用modify时, indexOf返回-1 ,因为equals的计算结果为false

This issue occurs if t2 is not in the ticketList - so there are two equal objects: one is t2 and the other is in the ticketList :如果t2不在ticketList中,则会出现此问题 - 因此有两个equal的对象:一个是t2 ,另一个在ticketList中:

static List<Ticket> ticketList = Arrays.asList(
    new Ticket(1, 1, "one", "one"), 
    new Ticket(2, 2, "two", "two"), 
    new Ticket(3, 3, "drei", "drei")
);
// ...
Ticket t2 = new Ticket(2, 2, "two", "two");

modify(t2, 0, "99999");
modify(t2, 0, "88888");

Most likely, methods equals and hashCode are overridden in class Ticket and they use the values of the member fields to define equality.最有可能的是,方法equalshashCode在 class Ticket中被覆盖,它们使用成员字段的值来定义相等性。

Then, method modify updates existing value toModify within the list, and possibly the ticketList is completely rebuilt inside calls to updateFile , readFile .然后,方法modify更新列表中的现有值toModify ,并且可能在对updateFilereadFile的调用中完全重建了ticketList

Thus, t2 becomes outdated because ticketList does not contain a ticket with old mainID any longer and indexOf returns -1.因此, t2变得过时了,因为ticketList不再包含具有旧mainID的票证并且indexOf返回 -1。

This can be overcome if the reference t2 is initially bound to some object within the ticketList -- then it becomes updated outside method modify :如果引用t2最初绑定到 ticketList 中的某个ticketList ,则可以克服这一问题——然后它会在方法modify外部更新:

Ticket t2 = ticketList.get(1);
modify(t2, 0, "99999");
modify(t2, 0, "88888");

You need to check if the index is equal to -1 then print a clear message and skip the process cause the ticket will not be there.您需要检查索引是否等于 -1,然后打印一条明确的消息并跳过该过程,因为票不存在。 indexOf returns -1 if there is no matching, so you could add this if:如果没有匹配,则 indexOf 返回 -1,因此您可以在以下情况下添加:

if(index < 0){
 System.out.print("ticket not exists!");
 break;
}

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

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