我正在学习如何在我的第二学期Java课程中使用链接列表。对于我们的计划,我们应该编写一个包含胜负的足球队理念的课程。
我需要帮助的部分是创建一种方法,该方法返回最多获胜的3支球队的昵称,另一支球队根据获胜百分比返回五支最佳球队。
团队成员
public class Team
{
private int GamesWon;
private int GamesLost;
private String Team;
public Team (int i, int j, String k)
{
GamesWon = i;
GamesLost = j;
Team = k;
}
public int getGamesWon()
{
return GamesWon;
}
public int getGamesLost()
{
return GamesLost;
}
public String getTeam()
{
return Team;
}
public void setGamesWon(int i)
{
GamesWon = i;
}
public void setGamesLost(int j)
{
GamesLost = j;
}
public void setTeam(String k)
{
Team = k;
}
public String toString()
{
return("Team: " + Team + "\tGamesWon: " + GamesWon + "\tGamesLost: " + GamesLost);
}
}
TeamNode类
public class TeamNode
{
private Team team;
private TeamNode next;
public TeamNode()
{
team = null;
next = null;
}
public TeamNode(Team t)
{
setTeam(t);
next = null;
}
public Team getTeam()
{
return team;
}
public TeamNode getNext()
{
return next;
}
public void setTeam(Team t)
{
team = t;
}
public void setNext(TeamNode tn)
{
next = tn;
}
}
ShellLinkedList
public abstract class ShellLinkedList
{
protected TeamNode head;
protected int numberOfItems;
public ShellLinkedList()
{
head = null;
numberOfItems = 0;
}
public int getNumberOfItems()
{
return numberOfItems;
}
public boolean isEmpty()
{
return (numberOfItems == 0);
}
public String toString()
{
String listString = "";
TeamNode current = head;
while (current != null)
{
listString += current.getTeam().toString() + "\n";
current = current.getNext();
}
return listString;
}
}
DataStructureExcetion(错误类)
public class DataStructureException extends Exception
{
public DataStructureException(String s)
{
super(s);
}
}
团队链接列表类
public class TeamLinkedList extends ShellLinkedList
{
public TeamLinkedList()
{
super();
}
public void insert(Team t)
{
TeamNode tn = new TeamNode(t);
tn.setNext(head);
head = tn;
numberOfItems++;
}
public Team delete(String searchName)
throws DataStructureException
{
TeamNode current = head;
TeamNode previous = null;
while (current != null && current.getTeam().getTeam() != searchName)
{
previous = current;
current = current.getNext();
}
if (current == null)
throw new DataStructureException(searchName + " not found: cannot be deleted");
else
{
if (current == head)
head = head.getNext();
else
previous.setNext(current.getNext());
numberOfItems--;
return current.getTeam();
}
}
}
客户端类
public class TeamLinkedListTest
{
public static void main(String [] args)
{
Team t1 = new Team (10, 0, "Patriots" );
Team t2 = new Team (9, 1, "49ners");
Team t3 = new Team (8, 2, "Chargers");
Team t4 = new Team (7, 3, "Bengals");
Team t5 = new Team (6, 4,"Jets");
Team t6 = new Team (5, 5, "Raiders");
Team t7 = new Team (4, 6, "Seahawks");
Team t8 = new Team (3, 7, "Cardinals");
Team t9 = new Team (2, 8, "Texans");
Team t10 = new Team(1, 9, "Dolphins");
TeamLinkedList Team = new TeamLinkedList();
System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());
Team.insert(t1);
System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());
Team.insert(t2);
System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());
Team.insert(t3);
System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());
Team temp;
try
{
temp = Team.delete("Bengals");
System.out.println("Team deleted: " + temp);
}
catch(DataStructureException dse1)
{
System.out.println(dse1.getMessage() + "\n");
}
try
{
temp = Team.delete("Patriots");
System.out.println("Deleted " + temp);
System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());
temp = Team.delete("49ners");
System.out.println("Deleted " + temp);
System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());
temp = Team.delete("Chargers");
System.out.println("Deleted " + temp);
System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());
temp = Team.delete("Patriots");
System.out.println("Deleted " + temp);
System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());
}
catch(DataStructureException dse2)
{
System.out.println(dse2.getMessage());
}
}
}
答案 0 :(得分:0)
简短回答。您可以使用Collections.sort
长(呃)回答。在您的Team类中,您必须覆盖compareTo
方法
class Team implements Comparable<Team> {
private gamesWon
//contructor/methods go here
@Override
public int compareTo(Team t) {
int wins = t.gamesWon;
if (this.gamesWon > wins) {
return 1;
} else if (this.gamesWon == wins) {
return 0;
} else {
return -1;
}
}
之后,您可以像这样在链接列表上调用sort方法
Collections.sort(myLinkedList)
最后,您可以打印出前三个节点,因为它们现在已经排序