Java 我的比较器类不工作

Java 我的比较器类不工作,java,arrays,comparator,Java,Arrays,Comparator,好的,我有一个包含多个实例变量的对象,我想按它排序,所以我读到我必须实现一个comparator类并使用它 基本上,它是一个以PlaneSeats对象为成员的平面类。 飞机座椅有SeatID,CustomerID 我想打印客户占用的座位,有人知道怎么打印吗 //This Prints it by SeatID (Since it starts from 0) for (int i = 0; i < seat.length; i++) { if (seat[i].i

好的,我有一个包含多个实例变量的对象,我想按它排序,所以我读到我必须实现一个comparator类并使用它

基本上,它是一个以PlaneSeats对象为成员的平面类。 飞机座椅有SeatID,CustomerID

我想打印客户占用的座位,有人知道怎么打印吗

//This Prints it by SeatID (Since it starts from 0)
 for (int i = 0; i < seat.length; i++) {
            if (seat[i].isOccupied()) {
                System.out.println("SeatID " + seat[i].getSeatID() + " assigned to CustomerID " + seat[i].getCustomerID());
            }
        }
//它由SeatID打印(因为它从0开始)
对于(int i=0;i
我失败的比较器代码如下:我希望不要使用单独的类,希望这样的数组排序。排序函数

import java.util.*;

public class Comparator implements Comparator<Plane> {
    public int compare(Plane CustomerID[], Plane CustomerID[]) {

    }
}
import java.util.*;
公共类比较器实现比较器{
公共整数比较(平面自定义ID[],平面自定义ID[]){
}
}

您不应该将类命名为“Comparator”,因为它已经是一个Java接口名称

Plane CustomerID[]
这是没有道理的

我没有回答你之前关于座位的问题吗? PlaneSeat类应该实现Comparable和一个名为

public int compareTo(PlaneSeat seat)
在这个方法中,seat是第二个seat,您正在与之比较的对象。另一个对象是

this
在这个方法中,您可以调用

getCustomerID()
对象上的方法。它应该是这样的:

public int compareTo(PlaneSeat seat) {
 if (this.getCustomerID() > seat.getCustomerID()) return 1;
 if (this.getCustomerID() < seat.getCustomerID()) return -1;
 return 0;
}
召唤

对座位进行排序。

Arrays.sort(CustomerID,new Comparator(){
Arrays.sort(CustomerID, new Comparator<Plane>() {
        public int compare(Plane p1, Plane p2) {
                           PlaneSeat ps1 = p1.getPlaneSeat();
                           PlaneSeat ps2 = p2.getPlaneSeat();
            return ps1.getSeatID().compareTo(ps2.getSeatID());
        }
    });
公共整数比较(平面p1、平面p2){ PlaneSeat ps1=p1.getPlaneSeat(); PlaneSeat ps2=p2.getPlaneSeat(); 返回ps1.getSeatID().compareTo(ps2.getSeatID()); } });

这将根据SeatId进行排序。如果您想根据CustomerId进行排序,请将getSeatID()替换为getCustomerID()。

您不能再次实现Comparator类,它被定义为一个接口,您必须通过命名不同的类来实现它

这不会编译

public class Comparator implements Comparator<Plane> {
    public int compare(Plane CustomerID[], Plane CustomerID[]) {

    }
}
公共类比较器实现比较器{
公共整数比较(平面自定义ID[],平面自定义ID[]){
}
}
你在违反合同

有关可能的解决方案,请参阅此代码

飞机座椅类别定义

public class PlaneSeat {
   //Create your custom comparator strategy 
   public static final Comparator<PlaneSeat> CUSTOMER_COMPARATOR = new CustomerComparator();

    //fields
    private final Integer customerID;

        public PlaneSeat(Integer customerID){
            this.customerID= customerID;
        }

    private static class CustomerComparator implements Comparator<PlaneSeat>{

        @Override
        public int compare(PlaneSeat o1, PlaneSeat o2) {
             return o1.customerID.compareTo(o2.customerID); 
        }

    }

       @Override
       public String toString() {
      return "PlaneSeat [customerID=" + customerID + "]";
       }

}
公共舱座位{
//创建自定义比较器策略
公共静态最终比较器CUSTOMER_Comparator=新CUSTOMER Comparator();
//田地
私有最终整数customerID;
公共飞机座位(整数客户ID){
this.customerID=customerID;
}
私有静态类CustomerComparator实现Comparator{
@凌驾
公共int比较(飞机座椅o1,飞机座椅o2){
返回o1.customerID.compareTo(o2.customerID);
}
}
@凌驾
公共字符串toString(){
返回“PlaneSeat[customerID=“+customerID+”]”;
}
}
飞机类

public class Plane{

    private List<PlaneSeat> seats;

    public List<PlaneSeat> getSeats() {
        return seats;
    }

    public void setSeats(List<PlaneSeat> seats) {
        this.seats = seats;
    }

        public void sortSeatsByCustomer(){
             Collections.sort(seats,PlaneSeat.CUSTOMER_COMPARATOR);
        }

        @Override
    public String toString() {
         return "Plane [seats=" + seats + "]";
    }

}
公共类平面{
非公开名单席位;
公众名单{
返回座位;
}
公众座位(列出座位){
这个座位=座位;
}
公共作废SortSatsByCustomer(){
集合。排序(座位、飞机座位、客户/比较器);
}
@凌驾
公共字符串toString(){
返回“飞机[seats=“+seats+”]”;
}
}
然后在客户端代码中:

 public static void main(String args []){
    List<PlaneSeat> seats = new ArrayList<>();
    for(int i =10;i>0;i--)
      seats.add(new PlaneSeat(i--));

    Plane plane = new Plane();
    plane.setSeats(seats);
    System.out.println(plane);//print before sorting
    plane.sortByCustomers();
    System.out.println(plane);//after sorting by customer
  }
publicstaticvoidmain(字符串参数[]){
列表席位=新的ArrayList();
对于(int i=10;i>0;i--)
增加(新飞机座位(i-);
平面=新平面();
飞机.固定座椅(座椅);
System.out.println(平面);//排序前打印
plane.sortByCustomers();
System.out.println(平面);//按客户排序后
}

等等,对不起,我是新来的,但这是否意味着我不需要comparator类?我只是把它放在Plane类中?不,你不需要比较器类,也不,你把它放在Seat类中,因为你在排序座位,而不是飞机。他没有PlaneSeat数组。那么PlaneSeat类的可比性如何呢。他有一个平面类数组,每个平面对象有一个PlaneSeat对象。如果我错了,请纠正我查看OP访问座椅元件的方式。座椅显然必须是一个阵列。另外,OP指的是“array.sort”。我只有一个平面,我想做的只是比较PlaneSeat的customerID并对它们进行相应的排序。ok..您只需将其更改为-Arrays.sort(seat,new Comparator(){public int compare(PlaneSeat ps1,PlaneSeat ps2){return ps1.getCustomerID().compareTo(ps2.customerID();});或者还有一种方法可以解决这个问题——通过使用类似接口实现PlaneSeat类来定义PlaneSeat类的自然顺序。
public class Plane{

    private List<PlaneSeat> seats;

    public List<PlaneSeat> getSeats() {
        return seats;
    }

    public void setSeats(List<PlaneSeat> seats) {
        this.seats = seats;
    }

        public void sortSeatsByCustomer(){
             Collections.sort(seats,PlaneSeat.CUSTOMER_COMPARATOR);
        }

        @Override
    public String toString() {
         return "Plane [seats=" + seats + "]";
    }

}
 public static void main(String args []){
    List<PlaneSeat> seats = new ArrayList<>();
    for(int i =10;i>0;i--)
      seats.add(new PlaneSeat(i--));

    Plane plane = new Plane();
    plane.setSeats(seats);
    System.out.println(plane);//print before sorting
    plane.sortByCustomers();
    System.out.println(plane);//after sorting by customer
  }