gpt4 book ai didi

java - 根据字符串中的数字对字符串进行排序,Java

转载 作者:行者123 更新时间:2023-12-01 11:39:42 24 4
gpt4 key购买 nike

我正在尝试使用字符串实现合并排序,每个字符串包含一个数字,想要根据该数字进行排序。这是我的代码,Employee.java 是我的构造函数,merg 是主要方法和合并排序。它使用数字数组而不是字符串数组工作我收到以下错误:

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;
}
}

合并文件

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);
}
}

最佳答案

首先我想问一下,你需要使用自己的归并排序方法吗?如果没有,你应该让你的类(class)实现 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++;
}
}

关于java - 根据字符串中的数字对字符串进行排序,Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29639141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com