2015-04-14 221 views
0

我試圖使用字符串實現合併排序,每個字符串都包含一個數字,要根據該數字進行排序。這裏是我的代碼,Employee.java是我的構造函數,merg是主要的方法和mergesort。它的工作使用數字數組而不是字符串,我得到以下錯誤的數組:根據字符串中的數字對字符串進行排序Java

reason: actual argument Employee[] cannot be converted to int[] by method invocation conversion 
1 error 


public class Employee 
{ 
    private String name; 
    private int idNumber; 
    private String department; 
    private String position; 

    Employee(String n, int id, String dept, String pos) 
    { 
     name = n; 
     idNumber = id; 
     department = dept; 
     position = pos; 
    } 
    Employee(int id) 
    { 
     idNumber = id; 
    } 
     public void setName(String n) 
    { 
     name = n; 
    } 
    public void setIdNumber(int id) 
    { 
     idNumber = id; 
    } 
    public void setDepartment(String dept) 
    { 
     department = dept; 
    } 
    public void setPosition(String pos) 
    { 
     position = pos; 
    } 
    public String getName() 
    { 
     return name; 
    } 
    public int getIdNumber() 
    { 
     return idNumber; 
    } 
public String getDepartment() 
    { 
     return department; 
    } 
    public String getPosition() 
    { 
     return position; 
    } 
    public String toString() 
    { 
     String str = "|Employee name: " + name 
      +"\n|Employee Identification: " + idNumber 
      +"\n|Employee Department: " + department 
      +"\n|Employee Postions: " + position; 
     return str; 
    } 
} 

MERG文件

public class merg 
{ 
    public static void main(String[] args) 
    { 
    Employee e1 = new Employee("Edward" , 3342, "Finance", "Consultant"); 
    Employee e2 = new Employee("Howard", 4452, "Human Resources", "Manager"); 
    Employee e3 = new Employee("Chelsea", 3354, "IT", "System Admin"); 
    Employee e4 = new Employee("Kevin", 2298, "Physical Plant" , "Janitor"); 


    Employee arr[] = new Employee[4]; 
    arr[0]=e1; 
    arr[1]=e2; 
    arr[2]=e3; 
    arr[3]=e4; 
    Employee arr1[] = arr; 
    System.out.println("Before Merge Sort: "); 
    for(int i=0; i<arr.length;i++){ 
     System.out.println(arr[i].toString()); 
    } 
    System.out.println("After Merge Sort: "); 
    MergeSort(arr1); 
    for(int i=0;i<arr1.length;i++){ 
     System.out.println(arr1[i].toString()); 
     } 
    } 
    public static void Merge(int[] L, int[] R, int[] A) 
    { 
     int nL = L.length; 
     int nR = R.length; 
     int i = 0; 
     int j = 0; 
     int k = 0; 

     while(i < nL && j < nR) 
     { 
     if(L[i] <= R[j]) 
     { 
      A[k] = L[i]; 
      k++; 
      i++; 
     } 
     else 
     { 
      A[k] = R[j]; 
      k++; 
      j++; 
     } 

     } 
     while(i < nL) 
     { 
     A[k] = L[i]; 
     i++; 
     k++; 
     } 
     while(j < nR) 
     { 
     A[k] = R[j]; 
     j++; 
     k++; 
     } 
    } 
    public static void MergeSort(int[] A) 
    { 
     int n = A.length; 
     int i, j,mid; 
     if(n < 2) 
     return; 

     mid = n/2; 
     int[] left = new int[mid]; 
     int[] right = new int[n - mid]; 

     for(i = 0; i < mid; i++) 
     left[i] = A[i]; 
     for(i = 0; i< n-mid; i++) 
     right[i] = A[i+mid]; 
     MergeSort(left); 
     MergeSort(right); 
     Merge(left, right, A); 
    } 
} 
+0

你'MergeSort'方法需要一個'INT []'爲參數工作。您正在傳遞'Employee []' –

+0

如何在我的Employee構造函數中使用int []來對特定字符串進行排序? @VinceEmigh – octain

+0

你的'Employee'構造函數沒有'int []';哪個'int []'你指的是? –

回答

1

首先我要問,你需要使用自己的合併排序方法?如果沒有,你應該有你的類實現implements Comparator<Employee>

@Override 
public int compare(Employee o1, Employee o2) { 
    return Integer.compare(o1.getIdNumber(), o2.getIdNumber()); 
} 

或者使用

Arrays.sort(arr, new Comparator<Employee>() { 
    @Override 
    public int compare(Employee o1, Employee o2) { 
     Integer.compare(o1.getIdNumber(), o2.getIdNumber()); 
    } 
}); 

它的方式Arrays.sort是歸併

如果你必須使用你自己的合併您排序需要修改您的合併排序方法,採取Employee[]而不是int[]並使用Employee[]的實例int[] 所以:

public static void MergeSort(Employee[] A) 
{ 
    int n = A.length; 
    int i, j,mid; 
    if(n < 2) 
     return; 

    mid = n/2; 
    Employee[] left = new Employee[mid]; 
    Employee[] right = new Employee[n - mid]; 

    for(i = 0; i < mid; i++) 
     left[i] = A[i]; 
    for(i = 0; i< n-mid; i++) 
     right[i] = A[i+mid]; 
    MergeSort(left); 
    MergeSort(right); 
    Merge(left, right, A); 
} 

然後修改合併到也Employee[]

public static void Merge(Employee[] L, Employee[] R, Employee[] A) 
{ 
    int nL = L.length; 
    int nR = R.length; 
    int i = 0; 
    int j = 0; 
    int k = 0; 

    while(i < nL && j < nR) 
    { 
     if(L[i].getIdNumber() <= R[j].getIdNumber()) 
     { 
     A[k] = L[i]; 
     k++; 
     i++; 
     } 
     else 
     { 
     A[k] = R[j]; 
     k++; 
     j++; 
     } 
    } 
    while(i < nL) 
    { 
     A[k] = L[i]; 
     i++; 
     k++; 
    } 
    while(j < nR) 
    { 
     A[k] = R[j]; 
     j++; 
     k++; 
    } 
} 
+0

我已經用Comparator做了它,試圖用我自己的mergesort方法做到這一點,謝謝你,我會試試這個! – octain

相關問題