0
이 링크 된 목록을 점수순으로 표시하려면 어떻게해야합니까? GUI에서 최상위 점수를 기준으로 정렬 한 다음 하위 점수가 가장 낮은 점수를 얻으려고 할 때 필요합니다. 또한 항목을 10 개로 제한 할 수있는 방법이 있는지 궁금합니다. 도움을 주시면 감사하겠습니다! 감사.단일 링크 목록을 내림차순으로 정렬
public class ScoreList {
private Player head; //reference to the head of the list
private Player tail; //reference to the tail of the list
int count;
public ScoreList() {
count = 0;
head = null;
tail = null;
}
public int getCount() {
return count;
}
public int size() {
int count = 0;
Player p = head;
while(p != null) {
count++;
p = p.next;
}
return count;
}
//method to add an item to the list - append to the tail of the list
public void add(String name, String score) {
// Create Player object
Player newPlayer = new Player(name, score);
if (head == null) {
head = newPlayer;
tail = head;
count++;
}
else {
//append to the end of the list
tail.setNext(newPlayer);
tail = newPlayer;
count++;
}
if(size() > 10) {
Player currentPlayer = head;
for (int i = 0; i < 9; i++) {
currentPlayer = currentPlayer.next;
}
currentPlayer.next = null;
}
}
// end add method
//method to let the user get the data from a node in the list
public String getItem(int index) {
String result = "";
String name = "";
String score = "";
Player curName;
if (count > 0 && index == 0) {
//return the Player info at the head of the list
name = head.getName();
score = head.getScore();
}
else if (index > 0 && index < count) {
curName = head;
for (int i = 1; i <= index; i++) {
curName = curName.getNext();
}
name = curName.getName();
score = curName.getScore();
}
result = "Player: " + name + " Score: " + score;
return result;
}
//nested inner class
public class Player {
private String player;
private String score;
private Player next;
public Player() {
player = "";
score = "";
next = null;
}
public Player(String artist, String title) {
this.player = artist;
this.score = title;
next = null;
}
public String getName() {
return player;
}
public String getScore() {
return score;
}
public Player getNext() {
return next;
}
public void setArtist(String player) {
this.player = player;
}
public void setTitle(String score) {
this.score = score;
}
public void setNext(Player next) {
this.next = next;
}
}
}
을하는 데 도움이됩니다. 방법에 대한 예는이 답변을 참조하십시오. http://stackoverflow.com/questions/11003155/change-priorityqueue-to-max-priorityqueue – kerryjj