有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在ArraysList对象中查找最小值

我是Java和Android开发的法语初学者。事实上,我试着做练习,但我被卡住了

我有一个对象(Players)的数组列表(playersList2),Players包含string(mFirstName)和int(mScore))

我做了一些收集,循环的尝试,我读了很多文档和论坛,但我没有找到一个解决方案,但我很确定这很容易

    Players players = new Players(mFirstname, mScore);

    mPreferences = getSharedPreferences(PREF_PLAYERS_LIST, MODE_PRIVATE);
    String fromJsonPlayersList = mPreferences.getString(PREF_PLAYERS_LIST, null);

    Gson gson = new Gson();
    ArrayList<Players> playersList2 = gson.fromJson(fromJsonPlayersList, new TypeToken<ArrayList<Players>>()
    {
    }.getType());

    /* find the weakiest players/

    if  //mscore is bigger than weakest players
    {//remove the weakest players and add this one
    }*/

我想在整个ArrayList中找到最小的mScore,以形成一个条件(如果当前的mScore>;最小玩家列表2将最脆弱的玩家列表2替换为当前玩家)

非常感谢


共 (3) 个答案

  1. # 1 楼答案

    假设这是你的球员课程:

        public class Players {
        String mFirstName;
        int mScore;
    
        public Players(String mFirstName, int mScore){
            this.mFirstName = mFirstName;
            this.mScore = mScore;
        }
    }
    

    这将得到你的最低分数:

    public static void main(String[] args) {
    
        ArrayList<Players> playersList2 = new ArrayList<>();
    
        for (int i = 0; i < 10; i++){
            Players player = new Players("name", i);
        }
    
        Players lowestScore = playersList2.get(0);
    
        for (Players p: playersList2) {
            if (p.mScore < lowestScore.mScore){
                lowestScore = p;
            }
        }
    
        System.out.println(lowestScore.mScore);
    }
    
  2. # 2 楼答案

    有多种方法可以做到这一点

    在开始解决方案之前,让我们先来看看这里的Player类:

    class Player {
        String firstName;
        int score;
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public int getScore() {
            return score;
        }
    
        public void setScore(int score) {
            this.score = score;
        }
    }
    

    一种方法是循环遍历ArrayList并找到得分最低的玩家,另一种方法是使用流API找到得分最低的玩家

    解决方案1:我们在列表中循环查找得分最低的玩家

    List<Player> players = new ArrayList<>();
    
    // .. add some players in the List players
    
    Player weakestPlayer = null;
    int minScore = Integer.MAX_VALUE;
    for(Player player : players) {
        if(player.getScore() < minScore) {
            weakestPlayer = player;
        }
    }
    

    解决方案2: 在java中使用流API是一个有点复杂的解决方案

    Player weakestPlayer = players
            .stream()
            .min(Comparator.comparing(Player::getScore))
            .orElseThrow(NoSuchElementException::new);
    

    该解决方案使用Java Stream API查找得分最低的玩家。这里我们使用min()方法来找到得分最低的玩家。min()方法需要一个比较器来帮助它比较分数,并决定哪个玩家的分数最低。下面的代码行获取int的比较器

    Comparator.comparing(Player::getScore)
    

    如果列表为空,那么它将抛出一个错误,因为它无法找到最低分数的玩家

  3. # 3 楼答案

    最简单的解决方案是使用简单的for循环,例如:

    Players minScorePlayer = playersList2.get(0);
    for (Players player: playersList2) {
       if (player.getScore() < minScorePlayer.getScore()) {
          minScorePlayer = player;
       }
    }
    //now minScorePlayer variable contains the reference to a player with the minimal score
    

    附:本代码旨在为您提供总体思路。你可能需要根据自己的需要进行调整