gpt4 book ai didi

java - LinkedList 的 Collections.sort() 不起作用

转载 作者:行者123 更新时间:2023-11-29 08:31:50 27 4
gpt4 key购买 nike

我正在尝试对 LinkedList 进行排序基于 Attribute优先级

飞行等级

/**
* @author Dylan
*
*/

/**
* A Class to hold information for flights and allow it to be manipulated
*
*/
public class Flight {
private String flightID;//FlightId eg(BA001)
Integer priority; // 1 = lowest | 9 = highest

/**
* A simple constructor to initialise sensible values to attributes
*/
public Flight() {
this.setFlightID("BA378"); //initialises the attributes to sensible values
this.setPriority(1); //initialises the attributes to sensible values
}
/**
* A param constrctor to initials attributes to values of params
* @param flightID
* @param priority
*/
public Flight(String flightID, Integer priority) {
this.flightID = flightID; // Initialises flightID to the value of the param
this.priority = priority;// initialises priority to the value of the param
}

/**
* A simple method to return the flightID
* @return
*/
public String getFlightID() {
return flightID; //returns flightID
}

/**
* A simple method to set the flightID
* @param flightID
*/
public void setFlightID(String flightID) {
this.flightID = flightID; //sets value of flightID to params
}

/**
* a simple method to return the priority of the flight
* @return
*/
public Integer getPriority() {
return priority;//returns priority of the flight
}

/**
* a simple method to set the priority of the flight
* @param priority
*/
public void setPriority(int priority) {
this.priority = priority;//sets the flight priority to value of params
}
/**
* A method to turn attributes into a sensible string
*/
public String toString() {
return "Flight [flightID= " + flightID + ", priority=" + priority + "]";//toString to change attributes into a string that's easy to read
}
}

PriorityFlightQueue1 类

import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;

public class PriorityFlightQueue1 extends AbstractFlightQueue implements Comparator<Flight> {
LinkedList<Object> flights = new LinkedList<Object>();

public void joinQueue(Flight f) {
flights.addLast(f);

}

public Object landFlight() {
return flights.removeFirst();
}

public int size() {
return flights.size();
}

public void clear() {
Iterator<Object> it = flights.iterator();//Initialise iterator to it
while(it.hasNext()) {//A While loop to check if there's another index after the current index
flights.removeFirst();//If the condition is true then it will remove index
}
}

public void display() {
for(Object f : flights) {//for each statement
System.out.println(f);//prints out f to console
}

}

public int compare(Flight f1, Flight f2) {
if(f1.getPriority() < f2.getPriority()){
return 1;
} else {
return -1;
}
}
}

方法来自 FlightTestClass ,这基本上就是把数据放到链表中了。我正在尝试使用 Collections.sort() 对数据进行排序正如你在下面看到的

public void testPriorityFlightQueue1() {
PriorityFlightQueue1 q = new PriorityFlightQueue1();

Flight f1 = new Flight("BA001", 3);//entering flight details into the linkedlist(queue)
Flight f2 = new Flight("NR273", 7);//entering flight details into the linkedlist(queue)
Flight f3 = new Flight("RA291", 1);//entering flight details into the linkedlist(queue)
Flight fref;


showPriorityFlightQueue1(q);
q.joinQueue(f1);
System.out.println(f1 + " has joined the queue to land");
showPriorityFlightQueue1(q);
q.joinQueue(f2);
System.out.println(f2 + " has joined the to land");
showPriorityFlightQueue1(q);
q.joinQueue(f3);
System.out.println(f3 + " has joined the to land");
showPriorityFlightQueue1(q);
System.out.println("\n-----------------------------------------\n");
fref = (Flight) q.landFlight();
System.out.println(fref + " has landed");
showPriorityFlightQueue1(q);
fref = (Flight) q.landFlight();
System.out.println(fref + " has landed");
showPriorityFlightQueue1(q);
fref = (Flight) q.landFlight();
System.out.println(fref + " has landed");
showPriorityFlightQueue1(q);
System.out.println("\n-----------------------------------------\n");

Collections.sort(q.flights);

showPriorityFlightQueue1(q);


}

我真的不确定为什么它不会对 LinkedList 进行排序在 Eclipse 中,建议框显示:

The method sort(List) in the type Collections is not applicable for the arguments (LinkedList)

然后它要我转换参数 q.flightsList<T> .

最佳答案

创建Flight比较器

public class Flight {
private String flightID; // FlightId eg(BA001)
Integer priority; // 1 = lowest | 9 = highest

public static final Comparator<Flight> SORT_MAX_PRIORITY = new Comparator<Flight>() {
@Override public int compare(Flight a, Flight b) {
return b.priority - a.priority;
}
};

public static final Comparator<Flight> SORT_MIN_PRIORITY = new Comparator<Flight>() {
@Override public int compare(Flight a, Flight b) {
return a.priority - b.priority;
}
};

public static final Comparator<Flight> SORT_BY_ID = new Comparator<Flight>() {
@Override public int compare(Flight a, Flight b) {
return a.flightID.compareTo(b.flightID);
}
};
// the rest of the class...
}

然后使用其中之一进行排序。

Collections.sort(q.flights, Flight.SORT_MAX_PRIORITY);

关于java - LinkedList 的 Collections.sort() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47394939/

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