This is my code, after initialized the array, I cannot able to reassign some value in array. It shows array index out of bound exception.
public class NewClass {
public static void main(String args[]){
String cl[]={};
cl[0]="10";
System.out.print(cl.length);
}
}
my output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at NewClass.main(NewClass.java:15)
Java Result: 1
String cl[]={};
Creates an instance of an empty array, so you can't add any elements to it.
In order to create a non empty array, use either
String cl[] = {"something",...};
or
String cl[] = new String[theArrayLength];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With