【发布时间】:2016-01-09 13:25:03
【问题描述】:
为什么会有 ArrayIndexOutOfBounds 异常..请澄清:) 我尝试更改数组的大小,但仍然无法创建成功的程序 导入 java.util.Scanner;
类插入 {
public static void main(String[]args) throws Exception
{
Scanner sc = new Scanner(System.in);
int a[]= new int[5];
int i;
for(i=0;i<a.length-1;i++)
{
System.out.println("Enter the Element : ");
a[i]=sc.nextInt();
}
System.out.println("Enter the location for insertion : ");
int loc = sc.nextInt();
System.out.println("Enter the value for location : " +loc+" is");
int value = sc.nextInt();
for(i=a.length-1;i>loc;i--)
{
a[i+1]=a[i];
}
a[loc-1] = value;
System.out.println("New Array is : ");
for (i=0;i<=a.length-1;i++)
{
System.out.println(a[i]);
}
}
}强文本
【问题讨论】:
-
a[i+1]在i = a.length-1时抛出此异常。 -
您是否尝试过调试问题?
-
应该是缓冲区溢出错误。
-
有两行可以抛出此异常 a[i+1] = a[i] 和 a[loc-1] = value。
标签: java arrays exception insert