Lỗi lỗi hệ thống java.lang.arrayindexoutofboundsexception 1 năm 2024
An array is a data structure that is the base for many advanced data structures e.g. list, hash table or a binary tree. The array stores elements in the contiguous memory location and it can also have multiple dimensions e.g. a two-dimensional array. You can use the 2D array to represent a matrix, a board in games like Tetris, Chess, and other board games. Also, knowledge of data structure and the algorithm is a must for any good programmer. You can also join these free data structure and algorithms courses to learn more about an array in Java. Understanding ArrayIndexOutOfBoundsExceptionThis error comes when you are accessing or iterating over an array directly or indirectly. Directly means you are dealing with array type e.g. String[] or main method, or an integer[] you have created in your program. Indirectly means via Collection classes that internally use an array e.g. ArrayList or HashMap. Now let's understand what information the associated error message gives us: java.lang.ArrayIndexOutOfBoundsException: 0 means you are trying to access index 0 which is invalid, which in turn means the array is empty. Here is a Java program that reproduces this error by accessing the first element of the empty array i.e. array with zero length: public class HelloWorldApp { }
Output
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
You can see the accessing first element of an empty array resulted in the ArrayIndexOutOfBoundsException in Java.java.lang.ArrayindexOutOfBoundsException: 1 means index 1 is invalid, which in turn means the array contains just one element. Here is a Java program that will throw this error: public class HelloWorldApp { }
When you run this program it will throw following error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
Similarly, java.lang.ArrayIndexOutOfBoundsException: 2 means index 2 is invalid, which means the array has just 2 elements and you are trying to access the third element of the array.Sometimes when a programmer makes a switch from C/C++ to Java they forget that Java does the bound checking at runtime, which is a major difference between C++ and Java Array. You should also join these Java Programming and development courses if you are learning Java and knows C++. The author often highlights the key difference between C++ and Java while teaching important concepts. How to avoid ArrayIndexOutOfBoundsException in JavaIn order to avoid the java.lang.ArrayIndexOutOfBoundsException, you should always do the bound check before accessing array element e.g. if (args.length < 2) { System.err.println("Not enough arguments received."); return; } Always remember that the array index starts at 0 and not 1 and an empty array has no element in it. So accessing the first element will give you the java.lang.ArrayIndexOutOfBoundsException : 0 error in Java. You should always pay to one-off errors while looping over an array in Java. The programmer often makes mistakes that result in either missing the first or last element of the array by messing 1st element or finishing just before the last element by incorrectly using the <, >, >= or <= operator in for loops. For example, the following program will never print the last element of the array. The worst part is there won't be any compile-time error or runtime exception. It's a pure logical error that will cause incorrect calculation. /
You can see the first element of the array i.e. 2 is never get printed. There is no compile-time or runtime error, though. Here are few handy tips to avoid ArrayIndexOutOfBoundsException in Java:
And, here is a nice slide of some good tips to avoid ArrayIndexOutOfBondsException in Java: That's all about how to solve the java.lang.ArrayIndexOutOfBoundsException: 1 in Java. Always remember that the array uses the zero-based index in Java. This means the index of the first element in the array is zero and if an array contains only one element then the array[1] will throw java.lang.ArrayIndexOutOfBoundsException : 1 in Java. Similarly, if you try to access the first element of an empty array in Java, you will get the java.lang.ArrayIndexOutOfBoundsException : 0 error in Java. Other Java troubleshooting guides for Programmers
|