2014-03-31 3 views
0

삽입을 사용하여 문자열 배열을 정렬하는 데 문제가 있습니다.삽입 정렬 방법을 사용하여 문자열 데이터의 배열 정렬

나는 다음과 같은 코드를 컴파일 할 때 :

cannot find symbol- method insert(java.lang.String[], int) 

나는 그것이 우리가 우리의 책을 사용하는 들었다는 사실 함께 할 수있는 뭔가가 생각 : 그것은 나에게 오류를 제공

public class Project1 { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     String names[]=new String[5]; 
     int size=names.length; 
     System.out.println("Enter the 5 car manufacturers: "); 
     //Load Array 
     for (int i = 0; i < 5; i++) { 
      names[i] = input.nextLine();    
     } 

     //Print descending order list 
     String[] descSort; 
     descSort=bubbleSortDesc(names); 
     System.out.println("Car manufacturers listed sorted in descending order (via BubbleSort): "); 
     for (int x=0; x < names.length; x++) { 
      System.out.println(names[x]); 
     } 

     //Print ascending order list 
     insertionSortAsc(names, size); 
     System.out.println("Car manufacturers listed sorted in ascending order (via InsertionSort): "); 
     for (int z=0; z < names.length; z++) { 
      System.out.println(names[z]); 
     } 
    }¨ 

    public static String[] bubbleSortDesc(String[] names) { 
     String temp; 
     int passNum, i, result; 
     for (passNum=1; passNum <= 4; passNum++) { 
      for (i = 0; i<=(4-passNum); i++) { 
       result=names[i].compareToIgnoreCase(names[i+1]); 
       if(result<0) { 
        temp=names[i]; 
        names[i]=names[i+1]; 
        names[i+1]=temp; 
       } 
      } 
     } 
     return names; 
    } 

    public static void insertionSortAsc(String[] names, int i) { 
     String temp = names[i]; 
     int j = i-1; 
     while (j >= 0 && names[j].compareToIgnoreCase(temp) > 0) { 
      names[j+1]=names[j]; 
      j--; 
     } 
     names[j+1]=temp; 
    } 

    public static void insertionSort(String[] names, int n) { 
     for(int i = 1; i<n; i++) { 
      insertionSortAsc(names, i); 
     } 
    } 
} 

을 코드에 대한 참조를 제공하지만이 책에서는 int 유형의 데이터 만 정렬하며 문자열 데이터 정렬 예제는 없습니다.

도움을 주시면 감사하겠습니다.

편집 : 오류를 수정 한 후, 프로그램을 컴파일하고 실행하는하지만이 충돌하고 나에게

java.lang.ArrayIndexOutofBoundsException: 
5 

이 오류는 라인 String temp = names[i]

답변

0

당신이하지 않은에게 강조 다음과 같은 오류가 제공하는 데이터를 입력 한 후 insert라는 메서드를 정의했습니다.

+0

나는 그것이, 입력 주셔서 감사합니다 책을 넣어 방식에서 사전로드 된 방법이었다 인상되었다. – user3385542

+0

그럴 수도 있지만 여전히 클래스 경로에 추가하거나 다시 구현해야합니다. 아마도 후자 일 것입니다. –

+0

데이터 입력 후 새로운 문제에 대한 의견이 있으십니까? – user3385542

0

이것은 당신이하고자하는 방식으로 작동합니다

public static void insertionSortAsc(String[] names, int n) 
{ 
    for(int i = 1; i<n; i++) 
    { 
     insert(names, i); 
    } 
} 

public static void insert(String[] names, int i) 
{ 
    String temp = names[i]; 
    int j = i - 1; 

    while (j >= 0 && names[j].compareToIgnoreCase(temp) > 0) 
    { 
     names[j + 1]= names[j]; 
     j--; 
    } 
    names[j + 1] = temp; 
}