我正在尝试构建一个java分类器,但是我一直得到一个错误 公共类分拣机{ 公共静态void main(字符串[]args){ int[]arr={9,3,5,6,2,2,53,5,75,78,68,6,5,44,563356242,25}; 对于(int i=0;i
它应该对数组进行排序,但我得到了这个错误。我真不知道该怎么办。我正在尝试使用选择排序算法:我正在尝试构建一个java分类器,但是我一直得到一个错误 公共类分拣机{ 公共静态void main(字符串[]args){ int[]arr={9,3,5,6,2,2,53,5,75,78,68,6,5,44,563356242,25}; 对于(int i=0;i,java,indexoutofboundsexception,Java,Indexoutofboundsexception,它应该对数组进行排序,但我得到了这个错误。我真不知道该怎么办。我正在尝试使用选择排序算法: java.lang.ArrayIndexOutOfBoundsException: 18 at Sorter.main(Sorter.java:8) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown So
java.lang.ArrayIndexOutOfBoundsException: 18
at Sorter.main(Sorter.java:8)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
一致
for (int j = i + 1; i < arr.length; j++)
输出:
[2, 2, 3, 5, 5, 5, 5, 6, 6, 9, 25, 44, 53, 68, 75, 78, 242, 63356]
考虑变量
i
。它从0
到数组的长度。您的数组有编号为0
到17
的18个元素。变量j
设置为i+1
的值。当外部循环中的i
为17
时,j
将设置为18
。索引18
(arr[18]
)超出了数组的末尾。在第二个for
中,您可能是指jAwesome,非常感谢。