我的比较器类不起作用

时间:2014-03-09 19:50:51

标签: java arrays comparator

好的,我有一个有多个实例变量的对象,我想排序,所以我读到我必须实现一个比较器类并使用它。

基本上它是一个PlaneSeats对象作为其成员的平面类。 PlaneSeats具有SeatID,CustomerID

我希望通过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());
            }
        }

我失败的比较器代码如下:我希望不要使用单独的类,希望这样的array.sort函数?

import java.util.*;

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

    }
}

4 个答案:

答案 0 :(得分:1)

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

这将基于SeatId排序。如果要基于CustomerId进行排序,请将getSeatID()替换为getCustomerID()。

答案 1 :(得分:1)

这不会编译

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

    }
}

你违反合同。

请参阅此代码以获取可能的解决方案。

PlaneSeat类定义

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 + "]";
       }

}

平面类

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
  }

答案 2 :(得分:0)

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

Plane CustomerID[]

这没有意义。

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

的方法
public int compareTo(PlaneSeat seat)

在这种方法中,座位是第二个座位,是您要比较的对象。另一个对象是

this

在此方法中,您可以调用

getCustomerID()

对象的方法。它应该看起来像:

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

如果这给出了你想要的相反顺序,则交换1和-1。 在您发表评论之前

//This Prints it by SeatID (Since it starts from 0)

呼叫

seat = Arrays.sort(seat);

对座位进行排序。

答案 3 :(得分:0)

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