This discussion is archived
5 Replies Latest reply: Oct 12, 2012 12:34 PM by 967860 RSS

ArrayList Question

967860 Newbie
Currently Being Moderated
Hello all,

I understand that the Index must be less than the Size of the ArrayList, or the typical error message will be given:

"Problem occurred during processing: Index 1, Size: 1"

However, given the For Loop below and its consideration of the rule. Why did my program terminate after the ArrayList reached a size of 96, giving me the following message?

"Problem occurred during processing: Index 96, Size 96"

The program ran great until the ArrayList size reached 96. What could cause the Index to catch up to the Size after 3 days of runtime?

for(i=0; i<a.size(); i++)
{

if(true) a.add(0);

}
  • 1. Re: ArrayList Question
    rp0428 Guru
    Currently Being Moderated
    >
    However, given the For Loop below and its consideration of the rule. Why did my program terminate after the ArrayList reached a size of 96, giving me the following message?
    >
    Post the code that you are using.

    Whether the for loop even does anything depends on how 'a' was created and initialized since a new ArrayList has a size of zero so the loop would not even execute unless you have first added something to the list.

    And then you do understand that ArrayList's expand their capacity as needed indefinitely?

    I run out of heap after about 25764000 iterations using this code
        int i;
        ArrayList a = new ArrayList();
        a.add(0);
            for(i=0; i<a.size(); i++)
            {
                if ((i % 1000) == 0) {
                    System.out.println(i);
                }
                if(true) a.add(0);
            }
    What is it you are trying to accomplish with the infinite loop? Are you trying run out of heap or trying to see what happens when the int reaches its maximum?
  • 2. Re: ArrayList Question
    967860 Newbie
    Currently Being Moderated
    Thank you for your quick reply.

    In response to your quoted statements, let me try to clarify.

    "Whether the for loop even does anything depends on how 'a' was created and initialized since a new ArrayList has a size of zero so the loop would not even execute unless you have first added something to the list."

    I initialized ArrayList 'a' and added the first element before the For Loop begins. For example:

    int i;
    ArrayList a = new ArrayList();
    a.add(0);

    for(i=0; i<a.size(); i++)
    {
         if(x-y > z) {
    a.add(0);
    }
    }

    "What is it you are trying to accomplish with the infinite loop? Are you trying run out of heap or trying to see what happens when the int reaches its maximum?"

    I am not trying to run out of heap space or reach a maximum. I am simply trying to figure out why the size of 'a' stopped growing at 96. Why did the size of 'a' not increase to 97, 98, 99, and so on. Instead I received the following message after the size of 'a' reached 96:

    "Problem occurred during processing: Index 96, Size 96"

    Do you think this message was caused because I ran out of heap space, or do you think it was caused by something else?
  • 3. Re: ArrayList Question
    EJP Guru
    Currently Being Moderated
    Moderator action: deleted your three duplicate posts, all made after you had received an answer here. Just one thread per question thanks.
  • 4. Re: ArrayList Question
    EJP Guru
    Currently Being Moderated
    "Problem occurred during processing: Index 96, Size 96"
    It is not possible for that code to throw an exception containing that message. There is no array access ergo no index out of bounds possible.
    do you think it was caused by something else?
    Exactly. It is caused by something else. Namely, code that is different from what you have posted here.
  • 5. Re: ArrayList Question
    967860 Newbie
    Currently Being Moderated
    Much appreciated thank you.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points