gpt4 book ai didi

Java 数组索引越界 4

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:11:29 26 4
gpt4 key购买 nike

<分区>

我是 Java 的新手,在运行正常的代码下面运行,但我得到一个数组索引超出绑定(bind)的异常。有人可以帮助理解为什么我会收到此异常吗?

public class array {
public static void main (String[] args)
{
int[] b = {1,2,3,4};
array ar = new array();
ar.process(b);
}
public int process (int[] a)
{
int i;
System.out.println("Length is: " +a.length);
for(i = 0; i < a.length ; i++) {
System.out.println("A is : " + a[i] + " I is" +i);
}
return a[i];
}
}

Exception

Length is: 4
A is : 1 I is0
A is : 2 I is1
A is : 3 I is2
A is : 4 I is3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at array.process(array.java:17)
at array.main(array.java:7)

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com