当访问超过预定义长度的数组元素时,会产生 Java ArrayIndexOutOfBoundsException。数组在其确认时进行估计,并且它们本质上是静态的。当我们定义数组时,元素的个数是固定的,从0开始。例如,如果有10个元素,数组会将第10第元素识别为第11第 元素,因为第一个元素是第 0th 元素。一开始,每个程序员都认为程序一旦执行,输出就有保证。但是系统提出的问题会阻止程序在运行时执行,这些问题称为异常。因此,当运行时发生 IndexOutOfBoundsException 时,它是由 RuntimeException 类产生的,而 RuntimeException 类又是 Main Exception 类的子类,所有这些都派生自 java.lang 包。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
当从 ArrayOutOfBoundsException 实现接口时,就会形成构造函数。 ArrayIndexOutOfBoundsException 构造函数有 3 种类型,
以下是提到的示例:
创建 ArrayOutOfBoundsException
代码:
public class Main { public static void main (String[] args) { String[] veggies = {"Onion", "Carrot", "Potato", "Tomato", "Cucumber", "Radish"}; for (int i=0; i<=veggies.length; i++) { System.out.print (veggies[i] + " "); } } }
输出:
在上面的程序中,我们看到名为 veggies 的数组由 6 个元素组成。一旦 for 循环迭代,它会考虑条件 I <=veggies.length,这意味着数组会考虑从第 0th 位置到第 6th 位置的元素,最后,在最后一次迭代中,值 i=6 超出了数组大小,因此,实现了 ArrayoutOfBoundsException。
访问负索引的 ArrayOutOfBoundsException
代码#1
public class Main { public static void main (String[] args) { Integer[] intArray = {5, 7, 9, 11, 13, 15}; System.out.println ( "First element: " + intArray[0]); System.out.println ( "Last element: " + intArray[-8]); } }
输出:
在这里,我们首先声明一个整数数组,然后我们研究访问数组中的各个元素。完成后,我们尝试通过给出命令 intArray[0] 来打印第一个元素。这会识别第 0th 元素,即 5,因此将其打印出来。另一方面,我们还提供了一个命令来将负索引打印为-8。因此,系统无法识别该索引,因此在输出中打印 ArrayOutOfBoundsException。
代码#2
public class MyClass { int x; public MyClass () { x = 5; } public static void main (String[] args) { MyClass myObj = new MyClass (); System.out.println (myObj.x); } }
输出:
每个程序员在实现数组索引和元素时都会犯一个错误。由于这些原因,发生了 ArrayIndexOutOfBoundsException。我们可以按照以下步骤来避免这些错误。
增强循环会迭代相邻的内存区域(例如数组),并且仅获取合法索引。从现在开始,当实现增强的 for 循环时,我们不必担心会被误导或非法索引。
代码:
class Main { public static void main (String[] args) { String[] fruits = {"Apple", "Mango", "Banana", "Watermelon", "Papaya"}; System.out.println ( ""); for (String strval:fruits) { System.out.print (strval + " "); } } }
输出:
我们在上面的程序中使用了增强的 for 循环来迭代水果数组。首先,我们描述数组中的 5 个元素,然后实现增强的 for 循环。这个循环会迭代直到到达数组末尾,我们不需要单独定义每个数组。因此,它仅遍历有效的索引或元素,并最终提供我们正在寻找的内容的准确输出。因此,我们可以通过利用这个增强的 for 循环来避免 ArrayIndexOutOfBoundsException。
Arrays reliably start with list 0 and not 1. Moreover, the last part in the array can be gotten to using the record ‘arraylength-1’ and not ‘arraylength’. Programming engineers should be mindful while using beyond what many would consider possible and, as such, keep up a key good way from ArrayIndexOutOfBoundsException.
Type-safe iterators do not toss an exception when they emphasise an assortment and make changes to it as they depict the assortment and roll out the improvements to it.
Code:
import java.util.HashMap; public class MyClass { public static void main (String[] args) { HashMap<String, String> countries = new HashMap<String, String> (); countries.put ( "India", "Delhi"); countries.put ( "Switzerland", "Bern"); countries.put ( "France", "Paris"); countries.put ( "Belgium", "Brussels"); System.out.println (countries); } }
Output:
Explanation: In the above program, we declare the string of cities and countries and declare that to the HashMap. The system then recognizes this assortment, iterates the code, and finally produces the output without throwing an ArrayIndexOutOfBoundsException.
The ArrayIndexOutOfBoundsException in Java usually occurs when we try to access the elements which are greater than the array length. These exceptions can be avoided by using enhanced for loops or proper indices. There are also exceptions called NullPointerExceptions that are not checked and go beyond RuntimeException; it does not urge the software engineer to utilize the catch square to deal with it. Therefore, we should keep in mind that we should not go beyond array limits and also avoid NegativeArraySizeException.
以上是Java ArrayIndexOutOfBoundsException的详细内容。更多信息请关注PHP中文网其他相关文章!