我試圖使用字符串實現合併排序,每個字符串都包含一個數字,要根據該數字進行排序。這裏是我的代碼,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);
}
}
你'MergeSort'方法需要一個'INT []'爲參數工作。您正在傳遞'Employee []' –
如何在我的Employee構造函數中使用int []來對特定字符串進行排序? @VinceEmigh – octain
你的'Employee'構造函數沒有'int []';哪個'int []'你指的是? –