java – 用于数组倒计时的反向循环
发布时间:2020-12-15 02:00:34 所属栏目:Java 来源:网络整理
导读:我收到错误.. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at Reverse.main(Reverse.java:20). 语法没有错,所以我不确定为什么编译时会出错? public class Reverse {public static void main(String [] args){ int i,j; Syst
我收到错误..
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at Reverse.main(Reverse.java:20). 语法没有错,所以我不确定为什么编译时会出错? public class Reverse { public static void main(String [] args){ int i,j; System.out.print("Countdownn"); int[] numIndex = new int[10]; // array with 10 elements. for (i = 0; i<11 ; i++) { numIndex[i] = i;// element i = number of iterations (index 0=0,1=1,ect.) } for (j=10; j>=0; j--){ // could have used i,doesn't matter. System.out.println(numIndex[j]);//indexes should print in reverse order from here but it throws an exception? } } } 解决方法
Java使用基于0的数组索引.当你创建一个大小为10的int new [10]的数组时,它会在数组中创建10个整数’单元’.索引是:0,1,2,….,8,9.
您的循环计数到索引,该索引小于11或10,并且该索引不存在. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |