簡體   English   中英

按特定值對 ArrayList 進行排序

[英]Sorting ArrayList by specific value

有沒有一種方法可以按我的團隊擁有的點數對我的團隊數組列表進行排序,所以哪個團隊得分最高,等等,請不要使用 comperator,因為我不明白如何使用它,在這里是我的代碼:

import java.util.*;
import java.io.*;

class team {
    public int teamNum;
    public int points = 0;
    public team(int x) {
       this.teamNum = x;
    }
    public team(int x,int y) {
        this.teamNum = x;
        this.points = y;
    }
}


public class Problem9 {
    public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(new File("Test9.txt"));
    ArrayList<team> teams = new ArrayList<>();
    int counter=0;

    while(in.hasNextLine()) {
            boolean found = false;
            String[] split = in.nextLine().split(" ");
            int n1 = Integer.parseInt(split[0]);
            int n2 = Integer.parseInt(split[1]);

            if (!(n1 == 0 && n2 == 0)) {
                if (counter<1) teams.add(new team(n1));
                for (int i=0; i<teams.size(); i++) {
                    if (teams.get(i).teamNum == n1) {
                       teams.get(i).points+=n2;
                       found = true;
                    }
                }
            if (!found) {
                teams.add(new team(n1, n2));
            }
        }
        counter++;
    }
    for (int k=0; k<teams.size(); k++)
        System.out.println(teams.get(k).teamNum + " " +  teams.get(k).points);
     }    
}

有兩種方法可以對自定義數據類型進行排序:

  1. 使用比較器
  2. 使用可比

使用可比

class team implements Comparable<team> {
    public int teamNum;
    public int points = 0;
    public team(int x) {
       this.teamNum = x;
    }
    public team(int x,int y) {
        this.teamNum = x;
        this.points = y;
    }

    public int compareTo(team t1){
     return t1.points - this.points;
    }
}

現在在arraylist上使用Collections.sort() ,它會為你排序。

同意@sschale,您不能僅僅因為您不知道如何使用它而不想使用它而逃避某些方法或庫。 為了幫助您,我將在這里為您提供一個非常簡化的比較器實現形式:

//Place this between class team and public class Problem9
static class rankcomparator implements Comparator<Team> {
    @Override //need to override
    public int compare(Team lhs, Team rhs) {
        return -compare(lhs.points, rhs.points); //if descending order
        //return compare(lhs.points, rhs.points); //if ascending order
    }
}

public static void main(String[] args) throws IOException {
    //Add this code inside public class Problem9 ...
    Collections.sort(teams, new rankcomparator());
}

您確實需要了解ComparatorComparable接口的工作原理。 我推薦這個關於Comparator使用的教程這個關於通過Comparable接口使用自然排序的教程

關於手頭的問題:只需向您的Team類添加一個getScore方法:

int getScore(){ return this.score;}

然后調用:

teams.sort(Comparator.comparing(Team::getScore));

您可以使用以下命令顛倒順序:

teams.sort(Comparator.comparing(Team::getScore).reversed());

最后一件事:類名以大寫字母開頭是慣例。 您應該將您的team類重構為Team

你也可以在沒有比較器的情況下這樣做,因為你只有整數來相互比較:

teams.sort((Team t1, Team t2) -> (Integer.compare(t1.getScore(), t2.getScore())));
Collections.reverse(teams);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM