0

I'm french beginner in Java and Android developpement. Actually I try to make an exercice but i'm stuck...

I have an ArrayList (playersList2) of Object (Players), Players contains string (mFirstName) and int (mScore))

I made some try with collection, loop, and I read lot of documentation and forum but i don'f find a solution however I'm pretty sure it's easy

    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
    }*/

I want to find the minimum mScore in the entire ArrayList to make a condition(if current mScore > minimal playersList2 replace weakiest PlayersList2 Players by current Players)

Thanks a lot

1
  • Actually , you have to iterate over the array list using such object as iterator.There are a few way to iterate over the array - using loops (for , while) and using iterator object. Commented Jun 23, 2019 at 15:00

3 Answers 3

1

Assuming this is your Players class:

    public class Players {
    String mFirstName;
    int mScore;

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

this would get your lowest score:

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

The easiest solution for you is to use a simple for-loop, something like:

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

P.S. this code is intended to give you the general idea. You may have to adjust it for your needs.

1 Comment

thanks for yours answers, actually it's seem working !
0

There are multiple ways to do this.

Before we start with the solution, let us have the Player class here:

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;
    }
}

One way is to loop through the ArrayList and finding the player with minimum score and other is using Stream API to find the player with minimum score.

Solution1: Here we loop through the List to find the Player with minimum score.

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;
    }
}

Solution2: It is a bit complex solution using Stream API in java.

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

This solution uses Java Stream API to find player with minimum score. Here we are using the min() method to find the player with minimum score. The min() method needs a comparator which will help it compare the scores and decide which player is having the minimum score. The below line of code fetches the comparator for int.

Comparator.comparing(Player::getScore)

If the List is empty then it will throw an error since it could not find the player with minimum score.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.