Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8968049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:22:59+00:00 2026-06-15T17:22:59+00:00

So I’m getting an index out of bounds exception on some code i’m writing.

  • 0

So I’m getting an index out of bounds exception on some code i’m writing. What I don’t understand is that I know for a fact that the index element I’m trying to work with exists.

Here’s the code:

I have a constructor for an array list

    public StixBoard(int number)
{
    stixGame = new ArrayList<Integer>(number);

    for (int i = 0; i < number; i++)
    {
        stixGame.add(i);
    }

}

This block generates a random variable 1-3

public int computeMove()
{

    int numberOfStix = (int) (3.0 * Math.random()) + 1;

    return numberOfStix;
}

Really straight forward, now here I have a method that takes the parameter supplied and attempts to remove those number of elements from the array list. As you can see, the parameter must be between 1 and 3, and it must be less than or equal to the size of the array list. Otherwise, the user is prompted to enter another number

public boolean takeStix(int number)
{
    boolean logicVar = false;
    placeHolder = stixGame.size();

    if ((number >= 1 && number <= 3) && number <= placeHolder)
    {
        for (int i = 0; i < number; i++)
        {
            stixGame.remove(i);
            logicVar = true;
        }
    } else if (number > 3 || number > placeHolder)
    {
        do
        {
            System.out
                    .println("Please enter a different number, less than or equal to three.");
            Scanner numberScan = new Scanner(System.in);
            number = numberScan.nextInt();
        } while (number > 3 || number > placeHolder);
    }

    return logicVar;
}

So as this program runs, the computeMove() method generates a random int (assuming the role of a computerized player) and attempts to translate that value to the number of indexes to be removed from the array list.

This ultimately brings me to this:

How many stix on the table? 4
|||||||||| 4 stix on the table
It's the computer's turn!
The computer chose 3

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.remove(ArrayList.java:387)
at StixBoard.takeStix(StixBoard.java:38)
at StixGame.main(StixGame.java:55)

So as you can see, the array list is of size 4, but when the computer rolls a 3, (which should leave me with 1), I am left with this error. How does my array list go from being of size 4 indexes to size 2?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:23:00+00:00Added an answer on June 15, 2026 at 5:23 pm

    You iterate through your list, from beginning to end, and remove an element at each step. This makes all the elements in the list shift to the left.

    First iteration: i = 0

    [1, 2, 3]
    

    Second iteration: i = 1

    [2, 3]
    

    Third iteration: i = 2

    [2] -> IndexOutOfBoudsException. There is no index 2 in this list.
    

    Iterate from the end to the beginning instead. That willmake it correct, and faster since the list won’t have to copy all the elements from right to left.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there's a lot of other questions out there that deal with this
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.