user2994118
user2994118

Reputation: 3

Java IndexOutOfBoundsException thrown, Unsure Why

This is my code:

import java.util.ArrayList;

public class PostmanProblem {
    public static void main(String[] args) {
        //Make the ArrayList and fill it
        ArrayList<Door> al = new ArrayList<Door>();
        for(int i = 0; i<=9; i++){
            al.add(new Door("closed", i));
        }

        //Do the problem
        for (int i = 1; i < 100; i++){
            for (int n = i; n < 100; n+=i){
                if (al.get(n).isClosed()){
                    al.get(n).open();
                }
                else
                {
                    al.get(n).close();    
                }
            }
        }

        for(Door d : al){
            System.out.print(d.toString() + "\t");
        }
    }
}

I'm not sure why, but I'm getting the following exception:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 10
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at PostmanProblem.main(PostmanProblem.java:15)

The line it refers to is the beginning of my if statement within the nested for loop. The Door class has the isClosed() method that returns a boolean, and also the isOpen() method that returns a boolean. It has void functions open() and close() which change its state to open or closed. The constructor for Door is Door(String state, int id). Hope that's all you need. Thanks in advance!

Upvotes: 0

Views: 666

Answers (3)

wanana
wanana

Reputation: 391

The size of your ArrayList al is 10(from index 0 to 9), but you are trying to access 1 to 100. So when the loop is in n==10, it's out of bounds.

Upvotes: 0

nhgrif
nhgrif

Reputation: 62052

Here's the thing:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 10
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at PostmanProblem.main(PostmanProblem.java:15)

This gives you a TON of information.

  • Line 15 of PostmanProblem.java. What's going on there?
  • java.util.ArrayList.get, there's a problem with the .get method you're calling on an ArrayList.
  • java.lang.IndexOutOfBoundsException: Index: 10, Size: 10

This is a LOT of information and it makes the problem mind-boggingly easy to solve.

First, go to the line where the error is occurring. Look at the indexes you're trying to access. Per what the program is actually supposed to be doing, are you accessing the right indexes? Or have you made a logic error here? Does it make sense that you should be able to access this index of this array for what the program is expected to do?

If this index shouldn't be accessed, then your error is RIGHT HERE. You need to fix the logic controlling what index is being accessed. However, if this is an index that should be able to be accessed, then the problem is you didn't make your ArrayList large enough. So now you need to go to the part of the code in which you've set up your ArrayList and figure out what logic error you've made here and why your ArrayList isn't as large as you intended it.


for(int i = 0; i<=9; i++){
    al.add(new Door("closed", i));
}

That's where you load values into al. You put ten values in. They're at indexes 0 through 9.

for (int i = 1; i <= 100; i++){
   for (int n = i; n <= 100; n+=i){
       if (al.get(n).isClosed()){
           al.get(n).open();
       } else {
           al.get(n).close();    
       }
   }
}

This is where you're going out of bounds. You're trying to get into index 10 (and way beyond), but your largest index is 9.

Upvotes: 1

vcetinick
vcetinick

Reputation: 2017

It looks like you're adding only 10 items into the ArrayList al and expecting to find 100??

 ArrayList<Door> al = new ArrayList<Door>();
 for(int i = 0; i<=9; i++){
      al.add(new Door("closed", i));
 }

However

 for (int n = i; n <= 100; n+=i){
    if (al.get(n).isClosed()){
         al.get(n).open();
    }

implies n will be > 9 at some stage

Upvotes: 0

Related Questions