我是Java和Android开发方面的法国初学者。其实我尝试做运动,但是我被困住了...

我有一个对象(播放器)的ArrayList(playersList2),播放器包含字符串(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>最小playersList2被当前Player替换为最弱的PlayersList2,则为Players)

非常感谢

最佳答案

对您而言,最简单的解决方案是使用简单的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


附言这段代码旨在为您提供总体思路。您可能需要根据需要进行调整。

09-26 18:52