이 프로그램은 기본 삽입 정렬입니다. 줄 번호 22 및 44에서 ArrayIndexOutOfBoundsException
을 얻고 있습니다.이 문제를 해결하는 데 협조 해주십시오. 삽입 방법에서 정렬 된 전체 배열을 반환하는 방법. 반품 a []이 (가) 작동하지 않습니다 .......................................... ....호출 메서드에서 배열 예외를 벗어났습니다. 메서드에서 전체 배열을 반환하는 방법
package analysis;
import java.util.*;
class Sort
{
int i,j,n,temp;
int a[] = new int[n];
int insertion(int a[],int n)
{
for (i=1;i<n;i++) {
j=i-1;
while (i>0 && a[j]>a[i]) {
temp = a[j];
a[j] = a[i];
a[i] = temp;
i--;
j--;
}
}
return a[n];
}
}
class Recurse
{
private static Scanner sc;
public static void main(String[] args)
{
System.out.println("enter array size");
int l,s;
sc = new Scanner(System.in);
s = sc.nextInt();
int b[] = new int[s];
int c[] = new int[s];
for (l=0;l<s;l++) {
b[l] = sc.nextInt();
}
Sort so = new Sort();
c[s] = so.insertion(b,s);
System.out.println("Sorted array is ");
for (l=0;l<s;l++) {
System.out.println(c[l]);
}
}
}
를 선언? –