Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java.lang.ArrayIndexOutOfBoundsException

import java.util.Random;
public class demo{
  public static void main(String[]args){
    Random rand=new Random();
    int array[]=new int[10];
    for(int i=0;i<array.length;i++){
      array[i]=rand.nextInt(100);//random between 0 and 100
    }
    System.out.println(array[array.length]);// error java.lang.ArrayIndexOutOfBoundsException
    // because index start 0 and end array.length
  }

}
Comment

java.lang.arrayindexoutofboundsexception: -1

check the code if it is accesing the element at negative index
Comment

ArrayIndexOutOfBoundsException

class AccessArrayOutsie
{
    public static void main (String[] args)
    {
        int[] array = new int[3];
        array[0] = 3;
        array[1] = 5;
  	array[2] = 6;
 
        for (int i = 0; i <= array.length; i++)
            System.out.println(array[i]);
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java get attributes from class 
Java :: java sample code 
Java :: polymorphism in oop 
Java :: java is list ordered 
Java :: mapping over a list of promises in javaascript 
Java :: java standard exception 
Java :: I/flutter (10109): {filePath: null, errorMessage: java.io.FileNotFoundException: open failed: EACCES (Permission denied), isSuccess: false} 
Java :: method reference in java 
Java :: loop through java object 
Java :: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification 
Java :: how to display the decimal value when 0 after point android studio 
Java :: how to calculate angle difference 
Java :: esponente in java 
Java :: how to use while loop in java 
Java :: set view size android 
Java :: java remove map 
Java :: Java alt + f4 
Java :: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.5.0:repackage failed: 
Java :: how to subtract localdatetime in java 
Java :: indexof an array in java 
Java :: rainbow six 
Java :: java list get first element 
Java :: getsource java 
Java :: print statement in java 
Java :: largest number in an array in java 
Java :: java localdate subtract two dates 
Java :: javafx fxmlloader location is not set 
Java :: jcolorchooser in java 
Java :: boolean in java 
Java :: what is static setter and getter examples in java 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =