比较数组列表中对象的int值,然后显示前3个对象

时间:2016-10-24 00:46:00

标签: java arraylist

我需要比较ArrayList玩家中的所有对象,并让它以本赛季总积分的形式返回前三名玩家(在TopPoints方法中)。

ArrayList中有5个玩家对象,他们的总分为:379,330,313,157和& 153.我知道我需要使用TotalPts来比较它们,但我要画一个空白。

任何人都可以帮助指导我朝正确的方向发展吗?

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class Roster {
    static ArrayList <Player> players = new ArrayList<Player> ();
    public static void main(String[] args) throws Exception {


        File file = new File("C:\\Users\\tpurv_000\\Desktop\\Stats.txt");
        Scanner scan = new Scanner(file);

        scan.useDelimiter("\\r?\\n");
        scan.nextLine(); // ignores first line, since it's a header

        while(scan.hasNextLine()) {
            String[] values = scan.nextLine().split("\t");
            Player currentPlayer = new Player();
            currentPlayer.setPlayerNum(Integer.parseInt(values[0]));
            currentPlayer.setPlayerName(values[1]);
            currentPlayer.setGP(Integer.parseInt(values[2]));
            currentPlayer.setGS(Integer.parseInt(values[3]));
            currentPlayer.setTotalMins(Integer.parseInt(values[4]));
            currentPlayer.setTotalFG(Integer.parseInt(values[5]));
            currentPlayer.setTotalFGA(Integer.parseInt(values[6]));
            currentPlayer.setThreeFG(Integer.parseInt(values[7]));
            currentPlayer.setThreeFGA(Integer.parseInt(values[8]));
            currentPlayer.setFT(Integer.parseInt(values[9]));
            currentPlayer.setFTA(Integer.parseInt(values[10]));
            currentPlayer.setOffReb(Integer.parseInt(values[11]));
            currentPlayer.setDefReb(Integer.parseInt(values[12]));
            currentPlayer.setPF(Integer.parseInt(values[13]));
            currentPlayer.setA(Integer.parseInt(values[14]));
            currentPlayer.setTO(Integer.parseInt(values[15]));
            currentPlayer.setSTL(Integer.parseInt(values[16]));
            currentPlayer.setBLK(Integer.parseInt(values[17]));
            currentPlayer.setTotalPts(Integer.parseInt(values[18]));
            players.add(currentPlayer);
        }
        System.out.println(players);
        TopPoints();

    }

    public static void TopPoints(){
        int largest = 0;
        int secondLargest = 0;
        int thirdLargest = 0;


        //Not sure how to compare




        System.out.println(largest);

    }


}

class Player {
    private int playerNum, GP, GS, totalMins, totalFG, totalFGA, threeFG, threeFGA, FT, FTA, offReb, defReb, PF, A, TO, STL, BLK, totalPts;
    private String playerName;


    //accessors & mutators
    public int getPlayerNum() {
        return playerNum;
    }

    public void setPlayerNum(int playerNum) {
        this.playerNum = playerNum;
    }

    public String getPlayerName() {
        return playerName;
    }

    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public int getGP() {
        return GP;
    }

    public void setGP(int gP) {
        GP = gP;
    }

    public int getGS() {
        return GS;
    }

    public void setGS(int gS) {
        GS = gS;
    }

    public int getTotalMins() {
        return totalMins;
    }

    public void setTotalMins(int totalMins) {
        this.totalMins = totalMins;
    }

    public int getTotalFG() {
        return totalFG;
    }

    public void setTotalFG(int totalFG) {
        this.totalFG = totalFG;
    }

    public int getTotalFGA() {
        return totalFGA;
    }

    public void setTotalFGA(int totalFGA) {
        this.totalFGA = totalFGA;
    }

    public int getThreeFG() {
        return threeFG;
    }

    public void setThreeFG(int threeFG) {
        this.threeFG = threeFG;
    }

    public int getThreeFGA() {
        return threeFGA;
    }

    public void setThreeFGA(int threeFGA) {
        this.threeFGA = threeFGA;
    }

    public int getFT() {
        return FT;
    }

    public void setFT(int fT) {
        FT = fT;
    }

    public int getFTA() {
        return FTA;
    }

    public void setFTA(int fTA) {
        FTA = fTA;
    }

    public int getOffReb() {
        return offReb;
    }

    public void setOffReb(int offReb) {
        this.offReb = offReb;
    }

    public int getDefReb() {
        return defReb;
    }

    public void setDefReb(int defReb) {
        this.defReb = defReb;
    }

    public int getPF() {
        return PF;
    }

    public void setPF(int pF) {
        PF = pF;
    }

    public int getA() {
        return A;
    }

    public void setA(int a) {
        A = a;
    }

    public int getTO() {
        return TO;
    }

    public void setTO(int tO) {
        TO = tO;
    }

    public int getSTL() {
        return STL;
    }

    public void setSTL(int sTL) {
        STL = sTL;
    }

    public int getBLK() {
        return BLK;
    }

    public void setBLK(int bLK) {
        BLK = bLK;
    }

    public int getTotalPts() {
        return totalPts;
    }

    public void setTotalPts(int totalPts) {
        this.totalPts = totalPts;
    }

    //Computing methods
    public int avgMin() {
        return (this.totalMins / this.GP);
    }

    public int fgPercentage() {
        return (this.totalFG / this.totalFGA) * 100;
    }

    public int threefgPercentage() {
        return (this.threeFG / this.threeFGA) * 100;
    }

    public int freethrowPercentage() {
        return (this.FT / this.FTA) * 100;
    }

    public int avgRebounds() {
        return (this.offReb + this.defReb) / 2;
    }
}

1 个答案:

答案 0 :(得分:0)

有很多解决方案。例如,您可以使用TreeList对所有项目进行排序。

要对ArrayList进行排序,您应该使用Collections.sort()方法。看看这个例子:

public class ArrayListSortExample{
    public static void main(String ... args){
        List<Player> players = new ArrayList<>();

        // Lets create 50 players with random score.
        Random random = new Random();
        for(int i = 0; i < 50; i++){
            Player player = new Player();
            player.setScore(random.nextInt(100));
            players.add(player);
        }

        // Lets sort players by score
        Collections.sort(players, new Comparator<Player>() {
            @Override
            public int compare(Player o1, Player o2) {
                return o1.getScore() - o2.getScore();
            }
        });

        // Print 3 top players
        for(int i = 0; i < 3; i++){
            System.out.println((i+1) + "º place: " + players.get(i).getScore() + " score");
        }
    }

    public static class Player{
        private int score;

        public int getScore() {
            return score;
        }

        public void setScore(int score) {
            this.score = score;
        }
    }
}