Java. lang. ArrayIndexOutOfBoundsException exception analysis and solution, java. lang

Source: Internet
Author: User

Java. lang. ArrayIndexOutOfBoundsException exception analysis and solution, java. lang

Reference: http://blog.csdn.net/javaeeteacher/article/details/4485834

Http://bbs.csdn.net/topics/90298133

This is a very common exception. In terms of names, it is an array subscript out-of-bounds error. The solution is to check why the subscript out-of-bounds.

The following is an error example:

Exception in thread "main" java. lang. ArrayIndexOutOfBoundsException: 2
At test4.State. nextStates (State. java: 93)
At test4.State. main (State. java: 478)

You can obtain the following information from the prompts:

1. The error occurred in 93 rows.

2. When an error occurs, the value of the lower mark is 2.

Next, we can analyze why the lower mark value is 2.

Your understanding:

This is actuallyArray out of boundsProblems

Array allocation in java is the space from 0 to its length-1,

For example:

int []a={1,2,3,4,5};

 

It allocates 5 spaces, which are defined as follows:

int [5]a={1,2,3,4,5}

 

Is the same, but you cannot call a [5], you can only call a [4]
Result: a [0] = 1, a [1] = 2, a [2] = 3, a [3] = 4, a [4] = 5,

But use: a [5] = array out of bounds


If the java program is running, an error occurs: javalangArrayIndexOutOfBoundsException: 0. Why? How can this problem be solved?

Java. lang. ArrayIndexOutOfBoundsException: 0
The array subscript is out of bounds even with 0. The only reason is that the array dimension is 0.
There are two types of array initialization statements that will cause this situation.
Int [] a = new int [0];
Int [] a = {};

If your program has been normal before and has become abnormal now, there is only one situation, that is, the int variable is used to initialize the array, as shown in figure
Int [] a = new int [I];
This I is a variable. When I> 0, the program is normal. When I is equal to zero and tries to access a [0], the exception you see will occur.

To solve this problem, you must first find the reason why the array dimension is initialized to 0.

JAVA exception javaLangStringIndexOutOfBoundsException

This exception is thrown by the String method, indicating that the index is negative or exceeds the size of the String. For some methods such as charAt, this exception is thrown when the index is equal to the size of the string.

What code are you using? You can post it to help you look at it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.