Java array problems. Could anyone help me?

Asked By 7060 points N/A Posted on -
qa-featured

How to handle java out of bounds array ,

What is its meaning anyway?

SHARE
Best Answer by fidanoski
Best Answer
Best Answer
Answered By 0 points N/A #80433

Java array problems. Could anyone help me?

qa-featured

Well, in Java everything is based on classes and inheritance, so there is a class called Exception which has job to tell you that you are making a mistake (your code has a bug). It means that you're trying to access element which is not in the bounds of your array.

Most frequently programmers make mistakes like yours because they don't know that when your working with arrays the index's start from 0 and the last element's index is n-1 where n is the number of elements in your array. So my advice is try your same code but lower the indexes for one and start from 0.

So to summarize, Array index out of bounds exception means that you're trying to access element whose index is out of the bounds of your array, to fix that count on the fact that there is one index less than the number of elements.

Answered By 0 points N/A #196853

Java array problems. Could anyone help me?

qa-featured
Hi
 
Let's say you have created one array of size 10 or say A[10]. That means you can store 10 values into this array from A[0] to A[9]. In Java, if you try to access an element A[10] or A[11] or any value which is greater than the size of the declared array, you will get "Array Index Out of Bound Exception".
 
In order to avoid this problem, make sure that you do not try to use an array element which your array doesn't have or is greater than the size of your array.
 
Hope it helps

Related Questions