Analysis of Java array out of bounds

Array initialization in Java is actually the same as OC, which is divided into dynamic initialization and static initialization,

Dynamic initialization: specify the length and the initialization value will be given by the system

Static initialization: the initialization value is given, and the length is given by the system

When we use arrays, the most common problem is array out of bounds. Well, here is a simple example

This is a two-dimensional array. Obviously, the array is out of bounds. The following information will be printed on the console:

Very accurate positioning to line 31.

Let's take a look at a complete code example:

The compiler displays an array out of bounds error.

It is found that there is an error in the underlined part of the statement

The following modifications should be made:

The reason for the error is that when the company constructor does not initialize array e, the size of array e is still 0, and an array out of bounds error occurs.

The test program written in the above error mode is as follows:

The output is 0. That is, the array size is zero.

summary

The above is the whole content of this article on the instance analysis of Java array cross-border problem. I hope it will be helpful to you. Interested friends can continue to refer to this site: sample code for judging Java exception exit conditions, Java programming exception handling best practices [recommended], etc. you can leave a message at any time if you have any questions, and Xiaobian will reply to you in time.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>