简体   繁体   中英

Why do I get this Exception error?

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at assg3_Tram.DVDCollection.remove(DVDCollection.java:60)
at assg3_Tram.DVDApplication.main(DVDApplication.java:95)

I start my program by selecting choice 4 (removes DVD object from list) in my switch/case. And I enter "Adam", which removes successfully. Then the menu repeats again, and I select 4 again to remove "Mystic River". That also removes successfully. Menu repeats again and I select 4, again. This time I type in "Mystic Rivers" (with an 's', to test that that DVD is not in the list), and that error pops up. I've included the relevant code and the .txt list I am reading from.

I am populating an ArrayList with the information from the .txt file. Each DVD object has 5 pieces of information. And each piece is one a separate line.

public DVD remove(String removeTitle) {
    for (int x = 0; x <= DVDlist.size(); x++) {
        if (DVDlist.get(x).GetTitle().equalsIgnoreCase(removeTitle)) { // This is line 60.
            DVD tempDVD = DVDlist.get(x);
            DVDlist.remove(x);
            System.out.println("The selected DVD was removed from the collection.");
            wasModified = true;
            return tempDVD;
        }
    }

    System.out.println("DVD does not exist in the current collection\n");
    wasModified = false;
    return null;
}

And in my Main class:

        case 4: {
            System.out.print("Enter a DVD title you want to remove: ");
            kbd.nextLine();
            String titleToRemove = kbd.nextLine();
            DVD dvdToRemove = dc.remove(titleToRemove); // This is line 95
            if (dvdToRemove != null) 
                System.out.println(dvdToRemove);
            System.out.print("\n");
            break;
        }   

The read in .txt file with the list.

Adam
Documentary
78 minutes
2012
7.99
Choo Choo
Documentary
60 minutes
2006
11.99
Good Morning America
Documentary
80 minutes
2010
9.99
Life is Beautiful
Drama
125 minutes
1999
15.99
Morning Bird
Comic
150 minutes
2008
17.99
Mystic River
Mystery
130 minutes
2002
24.99   

Problem is this:

for (int x = 0; x <= DVDlist.size(); x++) { ... }

You have to change it to

for (int x = 0; x < DVDlist.size(); x++) { ... }

Reason is that first item in your List is not at index 1 but 0. Indexing starting from 0 . Lists (like Java arrays) are zero based .

If your List has 10 items, last item is at position 9 and not 10. This is reason why you cannot use x <= DVDlist.size()

java.lang.IndexOutOfBoundsException: Index: 4, Size: 4

This means what i said. Your List has 4 elements but last element is at position 3 ie size - 1

0,1,2,3 --> COUNT = 4 // it starting from 0 not 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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