增强的“ for”循环导致ArrayIndexOutOfBoundsException

增强的“ for”循环导致ArrayIndexOutOfBoundsException,第1张

增强的“ for”循环导致ArrayIndexOutOfBoundsException

在这种情况下,

i
将分配给数组中的每个元素-它 不是 数组的索引

您想要做的是:

for(int i : arrs){    System.out.println(i);}

在您的代码中,您试图在迭代对象引用的数组索引处选择整数。换句话说,您的代码等效于:

for(int idx = 0; idx < arrs.length; idx++){    int i = arrs[idx];    System.out.println(arrs[i]);}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5477833.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存