#Array List

1 messages ยท Page 1 of 1 (latest)

potent trailBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

When you receive an "IndexOutOfBoundsException" error in Java, it means that you are trying to access an element in the array list at an index that is outside the valid range of indices for that array list.

Here are a few common reasons why you might be getting this error:

  1. Accessing an index that is greater than or equal to the size of the array list: Make sure that when you are accessing elements in the array list, you are using valid indices (i.e., indices between 0 and size-1).

  2. Adding elements to the array list without checking its size: If you are adding elements to the array list dynamically, make sure to check if the index at which you are adding the element is within the valid range.

  3. Modifying the size of the array list while iterating over it: If you are iterating over an array list and modifying its size during iteration, it can lead to IndexOutOfBoundsException. Consider using iterators or making a copy of the array list before iterating over it.

To fix this issue, carefully review your code and ensure that all accesses to the array list's elements are within its valid index range. You can also use try-catch blocks to handle IndexOutOfBoundsExceptions gracefully and prevent your program from crashing.

wooden urchin
somber aurora
#

Arrays are 0 index based, you define yours as having a capacity of 10, and you're asking for the 11th element (i = 10).

wooden urchin
#

yeah because the directions says to fill an array of 10 numbers

somber aurora
#

Yes, so your 10th number would be at index 9.

wooden urchin
#

oh so I do int[] h = new int[11]; and then put h[10] = h[9] + 10;

somber aurora
#

h[0] = number 1
h[1] = number 2
...
h[9] = number 10

#

No, that would be 11 numbers.

wooden urchin
#

oh

somber aurora
#

Arrays are 0 index based, so index 0 is your first number. Index 9 your tenth.

#

So rather than doing <= 10 you just need to do < 10.

wooden urchin
#

if I do that it shows this

#

what are they ๐Ÿ˜ญ

#

because I'm so confused

somber aurora
#

Instead you ask for input, and always increment that. So your sum's wrong.

wooden urchin
#

it works for those numbers

#

mb I just started this lesson and I wasn't there in class when my teacher taught it so I had to self teach

somber aurora
#

So it fails for the last number, what are you outputting currently, and have you tried to calculate it out yourself?

wooden urchin
#

ill try starting it at 1

somber aurora
#

I reread your code.

#

And there are two paths.

wooden urchin
#

that should work

#

oh I got 100

#

ty for your help ig

somber aurora
#

You're welcome.

potent trailBOT
#

Closed the thread.