我的问题在于第五个功能。我没有得到如何解决它。有人能帮助我吗?
List<Player>populate() throws ClassNotFoundException, SQLException;
void sortRuns(List<Player> list);
void sortByDebut(List<Player> list);
List<Player>sortByAverage(List<Player>list);
Set<Player> randomSet(List<Player> list,int n);
豆类是:
public class Player{
int capid;
String playerName;
Country country;
int matches;
int runs;
Date Debut;
int notOut;
float average;
}
Country as enum:
public enum Country {
India,Australia,SouthAfrica,NewZeland,England,SriLanka,WestIndies
}
Above Solved 4 functions are:
public class DataManagerImpl implements DataManager
{
DBConnectionImpl dm=new DBConnectionImpl();
Connection conn;
@Override
public List<Player> populate() throws ClassNotFoundException, SQLException {
List<Player> list=new ArrayList<Player>();
conn=dm.getConnection();
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("Select * from stats");
while(rs.next())
{
Player p=new Player(rs.getInt(1),rs.getString(2),Country.valueOf(rs.getString(3)),rs.getInt(4),rs.getInt (5),rs.getDate(6),rs.getInt(7));
list.add(p);
}
return list;
}
@Override
public void sortRuns(List<Player> list) {
// TODO Auto-generated method stub
Collections.sort(list,new Comparator<Player>(){
@Override
public int compare(Player o1, Player o2) {
// TODO Auto-generated method stub
return Integer.compare(o2.getRuns(),o1.getRuns());
}
});
}
@Override
public void sortByDebut(List<Player> list) {
// TODO Auto-generated method stub
Collections.sort(list,new Comparator<Player>(){
@Override
public int compare(Player o1, Player o2) {
// TODO Auto-generated method stub
return o1.getDebut().compareTo(o2.getDebut());
}
});
}
@Override
public List<Player> sortByAverage(List<Player> list) {
List<Player> plist=new ArrayList<Player>();
float average;
for(Player p:list)
{
int runs=p.getRuns();
int matchs=p.getMatches();
average=(runs/matchs);
p.setAverage(average);
plist.add(p);
}
Collections.sort(plist,new Comparator<Player>(){
@Override
public int compare(Player o1, Player o2) {
// TODO Auto-generated method stub
return Double.compare(o2.getAverage(),o1.getAverage());
}
});
// TODO Auto-generated method stub
return plist;
}
答案 0 :(得分:0)
所以这就是我如何理解这个问题。 这可能不会运行,但应该让您知道如何操作。
Set<Player> randomSet(List<Player> list,int n){
ArrayList<Player> possiblePlayers = new ArrayList<>(); // fist we create a list where we can put all players that have 10000 runs
for(Player player: list){ // we loop through the list
if(player.getRuns()> 10000){ // check each player if he had more than 10000 runs
possiblePlayers.add(player); // and if so, we add him to our list of possible results
}
}
// at this point we have a list of players that have more than 10000 runs and we need to randomly select a player and put him into the new Set
// so first we create a Set that we can return
Set<Player> resultSet = new HashSet<>(); // Set is abstract and needs an implemented class like HashSet. This may not be the most appropriate implementation for your case, but it will do for now.
// now we need to pick n random players from our list. So we actually generate n numbers that we use as indexes
Random random = new Random(); // first we instantiate our random generator
ArrayList<Integer> indexes = new ArrayList<>(); // we will use this list to store the generated indexes, so we can check that we don't pick the same index twice
while (n > 0) {
Integer randomIndex = random.nextInt(possiblePlayers.size()); // first we pick a number between 0 and the size of our possiblePlayers list
if (!indexes.contains(randomIndex)) { // then we check if that number is already in the indexes list
indexes.add(randomIndex); // if not, we add it
n--; // and decrement n;
}
// if the random number where already in the indexes list, the loop would simply continue and try again
}
// here we have now a lost of randomly generated (and unique) indexes
// now we get the players at that indexes and put them in the result set
for(Integer index: indexes){
resultSet.add(possiblePlayers.get(index));
}
// and finally return the set
return resultSet;
}
正如我之前所说,我不确定这种方法是否运行没有错误。 (我没有测试过)。但它会为您提供解决方案。不能保证它是一个很好的解决方案,但是:)正如你所看到的,为这么小的任务创建了很多列表和对象。我发现很有可能使它更有效率。