Java 使用对象数组进行插入排序?

Java 使用对象数组进行插入排序?,java,arrays,sorting,object,insertion-sort,Java,Arrays,Sorting,Object,Insertion Sort,我有一组教员,我想在上面使用插入排序。 我在以下位置得到一个错误: InsertionSort.insertionSortA(faculty, faculty.length); 错误显示:“类InsertionSort中的方法insertionSortA无法应用于给定类型; 必填项:可比[],整数 发现:教员[],国际 我知道这样做是行不通的: InsertionSort.insertionSortA((Comparable[]) faculty, faculty.length); 我知

我有一组教员,我想在上面使用插入排序。 我在以下位置得到一个错误:

InsertionSort.insertionSortA(faculty, faculty.length);
错误显示:“类InsertionSort中的方法insertionSortA无法应用于给定类型;
必填项:可比[],整数
发现:教员[],国际
我知道这样做是行不通的:

InsertionSort.insertionSortA((Comparable[]) faculty, faculty.length);
我知道如果我有一个整数[]数组可以工作,但我很困惑为什么我的函数[]不能工作

public class Tester {

public static void main(String[] args) {

    InsertionSort insertionSort = new InsertionSort();
    Education edu = new Education ("BA", "Business", 1);
    Education edu2 = new Education ("BA", "Health Science", 1);
    Education edu3 = new Education ("BA", "Computer Science", 1);

    Faculty[] faculty  = new Faculty[] { 

    new Faculty ("538", "Doe", "Jane", 'M', 1994,1,10, 
     "Assistant", edu),
    new Faculty ("238", "Do", "John", 'F', 1994,6,1, 
     "Assistant", edu2),
    new Faculty ("080", "White", "Snow", 'F', 1994,4,22, 
     "Full", edu3)
    };

    InsertionSort.insertionSortA(faculty, faculty.length);
}
}

公共类插入排序{
公共静态无效插入ORTA(可比[]阵列,整数n){
for(int unsorted=1;unsorted0)和&(数组[loc-1]。与(nextItem)>0)相比){
阵列[loc]=阵列[loc-1];
loc——;
}//结束时
阵列[loc]=下一个;
}//结束
}//结束插入排序
公共静态无效插入ORTB(可比[]阵列,整数n){
for(int unsorted=1;unsorted0)和&(数组[loc-1]。与(nextItem)<0)相比){
阵列[loc]=阵列[loc-1];
loc——;
}//结束时
阵列[loc]=下一个;
}//结束
} 
}

为了在数组/集合上应用插入排序,元素需要具有可比性(比较逻辑基于您想要比较两名教员的方式,可以基于姓名、年龄、薪水等)。这可以通过在教员类中实现接口Compariable并定义函数Comparieto()来实现详情如下:

public class Faculty implements Comparable<Faculty>
{
   public int compareTo(Faculty f)
   {

   // comparison logic is on the basis of how you want to compare 2 faculty members 
   //  you might want to compare name, salaries etc

   }
}

请参阅以下

中的文档,而不必查看
教员
课程,我们只能猜测您的问题

似乎
函数
没有实现
可比
。您可以使用
整数[]
调用该函数,因为
整数
是可比的——它确实实现了
可比

您需要在
Faculty
类中实现
Comparable
,方法是覆盖
compareTo(Faculty)


至于它应该返回什么,您应该检查它。但一般来说,如果它们完全相等,则返回
0
,如果
this
大于
faculty
(无论您定义“更大”是什么意思),它应该返回大于0的值。除此之外,没有任何严格的规则,除了它总是返回一致的值。

对于那些仍然在这个页面上跌跌撞撞的人,另一种解决方案是忽略可比较的接口。因为在很多情况下,一个对象可以按多个属性排序ibute,例如id、名字、姓氏或起始年份。在这种情况下,您将为InsertionSort类中的每个排序属性创建单独的方法。这还需要您将排序方法输入参数类型从Comparable[]更改为Faculty[]

public class InsertionSort {

  public static void byLastName(Faculty[] theArray, int n) {

    for (int unsorted = 1; unsorted < n; ++unsorted) {
      Faculty nextItem = theArray[unsorted];
      int loc = unsorted;
      while ((loc > 0) &&(theArray[loc-1].getLastName().compareTo(nextItem.getLastName()) > 0)) {
        theArray[loc] = theArray[loc-1];
        loc--;
      } 
      theArray[loc] = nextItem;
    }
  }

  public static void byFirstName(Faculty[] theArray, int n) {

    for (int unsorted = 1; unsorted < n; ++unsorted) {
      Faculty nextItem = theArray[unsorted];
      int loc = unsorted;
      while ((loc > 0) &&(theArray[loc-1].getFirstName().compareTo(nextItem.getFirstName()) > 0)) {
        theArray[loc] = theArray[loc-1];
        loc--;
      } 
      theArray[loc] = nextItem;
    }
  }
}

<>为了更详细地说明用插入排序对对象进行排序,请检查我的.I.在爪哇、C++、Python和JavaScript中实现。
 zero             : If object is same as the specified object (f)
 positive integer : If object is greater than the specified object (f)
 negative integer : If object is less than the specified object (f)
public int compareTo(Faculty faculty)  {
    //...
}
public class InsertionSort {

  public static void byLastName(Faculty[] theArray, int n) {

    for (int unsorted = 1; unsorted < n; ++unsorted) {
      Faculty nextItem = theArray[unsorted];
      int loc = unsorted;
      while ((loc > 0) &&(theArray[loc-1].getLastName().compareTo(nextItem.getLastName()) > 0)) {
        theArray[loc] = theArray[loc-1];
        loc--;
      } 
      theArray[loc] = nextItem;
    }
  }

  public static void byFirstName(Faculty[] theArray, int n) {

    for (int unsorted = 1; unsorted < n; ++unsorted) {
      Faculty nextItem = theArray[unsorted];
      int loc = unsorted;
      while ((loc > 0) &&(theArray[loc-1].getFirstName().compareTo(nextItem.getFirstName()) > 0)) {
        theArray[loc] = theArray[loc-1];
        loc--;
      } 
      theArray[loc] = nextItem;
    }
  }
}
InsertionSort.byLastName(faculty, faculty.length);
InsertionSort.byFirstName(faculty, faculty.length);