los cheques
intenta invalidar el
mostrando este error y los errores se manejan y muestran usando el bloque try-catch .
Java
// Java program to show the ArrayIndexOutOfBoundsException // while accessing element at negative index import java.io.*; class GFG { public static void main(String[] args) { try { int array[] = new int[] { 4, 1, 2, 6, 7 }; // accessing element at index 2 System.out.println("The element at index 2 is " + array[2]); // accessing element at index -1 // this will throw the // ArrayIndexOutOfBoundsException System.out.println("The element at index -1 is " + array[-1]); } catch (Exception e) { System.out.println(e); } } }
Producción
The element at index 2 is 2 java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 5
Acceso al elemento en el índice mayor que el tamaño de la array -1
Java
// Java program to show the ArrayIndexOutOfBoundsException // while accessing element at index greater then size of // array -1 import java.io.*; class GFG { public static void main(String[] args) { try { int array[] = new int[] { 4, 1, 2, 6, 7 }; // accessing element at index 4 System.out.println("The element at index 4 is " + array[4]); // accessing element at index 6 // this will throw the // ArrayIndexOutOfBoundsException System.out.println("The element at index 6 is " + array[6]); } catch (Exception e) { System.out.println(e); } } }
Producción
The element at index 4 is 7 java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 5
Publicación traducida automáticamente
Artículo escrito por lavishgarg26 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA