1

I am trying to create a array with Java that can hold as many numbers as the index 'i' is big.

for (int i = 0; i <= 10; i++)
    {
        int[] zahlenListe = new int[i];
        zahlenListe[i] = i + 5;
        System.out.println(zahlenListe[i]);

    }

but I am always getting the error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Start.main(Start.java:27)

Java:27 is this line of code: zahlenListe[i] = i + 5;.

But everything is working fine when I change this line

int[] zahlenListe = new int[i];

to this:

int[] zahlenListe = new int[11];

Anybody cares to explain where the error is?

6 Answers 6

8

Array indices are zero based. Hence the maximum index for i sized array is i-1.

Sign up to request clarification or add additional context in comments.

4 Comments

After seeing a typo and then an edit, I have to note that the plural of "index" if technical contexts is "indices"
@Arc676 Edited and followed by old school :)
@sᴜʀᴇsʜᴀᴛᴛᴀ indexes and indices are both valid English, but indexes is used more in technical context, all db documentations refer to them as indexes
@SleimanJneidi Both are valid and indices take less calories to pronounce. Just try ;)
5

An array of length i doesn't have an i'th index. The valid indices go from 0 to i-1.

If you initialize your array with int[] zahlenListe = new int[i+1];, you'll be able to assign a value to zahlenListe[i].

Comments

1
int[] zahlenListe = new int[i];
zahlenListe[i] = i + 5;

Arrays start at index 0.

So index i will never be in an i-dimensional array. It stops at i-1.

For i == 0 the array is empty (no entries at all).

You may want to start your loop at i=1.

From your code it is also not clear why you need an array at all (but you probably have more code in there that you are not showing).

Comments

1

If you creating an array using:

int[] zahlenListe = new int[i];

The last element in array zahlenListe would be zahlenListe[i-1] instead of zahlenListe[i]. In addition, assuming i should start with 1 instead of 0 because an array of length is pointless.

Therefore, use

zahlenListe[i-1] = i + 5;
System.out.println(zahlenListe[i-1]);

Comments

1

You need to start 0 to size-1 in an array as you see;

int[] zahlenListe = new int[i];

Your array size is always i which means you can allowed to access max i-1

Assuming that i always bigger than 0

Comments

-1

Basically because in your condition you are telling the computer to go to the 11th element. Think of it this way ...

int i = 0; 

First iteration i = 0
if i <= 10; then i = i + 1

Second iteration i = 1
if i <= 10; then i = i + 1

Third iteration i = 2
if i <= 10; then i = i + 1

Fourth iteration i = 3
if i <= 10; then i = i + 1

Fifth iteration i = 4
if i <= 10; then i = i + 1

Sixth iteration i = 5
if i <= 10; then i = i + 1

Seventh iteration i = 6
if i <= 10; then i = i + 1

Eighth iteration i = 7
if i <= 10; then i = i + 1

Ninth iteration i = 8
if i <= 10; then i = i + 1

Tenth iteration i = 9
if i <= 10; then i = i + 1

Eleventh iteration i = 10
if i <= 10; then i = i + 1

Once you get here you are trying to access an element that does not exist. Because your condition says that as long as i is less that or equal that 10, it should repeat the task. If you change <= for < then you stop at the last element available.

3 Comments

The eleventh iteration is not a problem (at least not more than the first one is).
Not sure what you mean, Explain!
The current code crashes in its first iteration, when i==0. If you fix that problem, it will also work for i==10.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.