Java 选择排序.ArrayIndexOutOfBoundsException
当我运行这个程序时,我得到了“线程中的异常”main“java.lang.ArrayIndexOutOfBoundsException:6”错误。这个代码是用来实现选择排序的。请有人帮助我Java 选择排序.ArrayIndexOutOfBoundsException,java,data-structures,Java,Data Structures,当我运行这个程序时,我得到了“线程中的异常”main“java.lang.ArrayIndexOutOfBoundsException:6”错误。这个代码是用来实现选择排序的。请有人帮助我 package Sorting; public class Selection { public static void sort(int[] array) { int min, temp; for (int i = 0; i <= array.lengt
package Sorting;
public class Selection {
public static void sort(int[] array) {
int min, temp;
for (int i = 0; i <= array.length-1 ; i++) {
min = i;
for (int j = i+1; j <= array.length ; j++) {
if ( array[j]<array[min]) {
min = j;
}
}
temp = array[min];
array[min]=array[i];
array[i] = temp;
}
}
public static void main(String[] args) {
int a[] = {51,6,3,55,34,12 };
sort(a);
for (int i :a) {
System.out.println(a[i]);
}
}
}
包裹分拣;
公开选课{
公共静态无效排序(int[]数组){
最低温度;
for(int i=0;iHey for each不返回索引,而是返回每个元素的返回值,因此您应该在下面打印结尾
for (int i :a) {
System.out.println(i);
}
这是正确的代码
public class Selection {
public static void sort(int[] array) {
int min, temp;
for (int i = 0; i <= array.length-1 ; i++) {
min = i;
for (int j = i+1; j <= array.length-1 ; j++) {
if ( array[j]<array[min]) {
min = j;
}
}
temp = array[min];
array[min]=array[i];
array[i] = temp;
}
}
public static void main(String[] args) {
int a[] = {51,6,3,55,34,12 };
sort(a);
for (int i=0;i<a.length ;i++) {
System.out.println(a[i]);
}
}
}
公共类选择{
公共静态无效排序(int[]数组){
最低温度;
对于(int i=0;ij