0

I found a task for Java beginners. It is a interface which i have to implement. I decided to use HashMap for Players, but now i noticed that i have to return an array of Player, right? Can you help me to understand how can i do it in getAllPlayers() method, please? Thank you

public class LeagueManager implements Manager{
Map<String, Player> players = new HashMap<String, Player>();
public void addPlayer(Player player) {
    players.put(player.getNick(), player);
}

public void removePlayer(Player player) {
    if (!players.isEmpty()) {
        players.remove(player.getNick());
    }
}

public Player getPlayer(String name) {
    if (!players.isEmpty() && players.containsKey(name)) {
        return (Player) players.get(name);
    } else {
        System.out.println("Error: there is no player with nick " + name);
        return null;
    }
}

public Player[] getAllPlayers() {
    if (!players.isEmpty()) {
        return null;
    } else {
        return null;
    }
}


public void addPoints (String name, int points) {
    if (players.containsKey(name)) {
        Player pl = (Player) players.get(name);
        pl.setPoints(points);
    }
}}
  • 6
    duplicate of http://stackoverflow.com/questions/1090556/java-how-to-convert-hashmapstring-object-to-array – JP Moresmau Jun 23 '15 at 18:00
  • You don't need to check if the collection is empty before calling remove(...) or before calling containsKey(...) or get(...). Also calling containsKey(...) and then get(...) is a waste of resources on a Map where null is not a relevant value, you can just get(...) and check if the result is not null. – Nyamiou The Galeanthrope Jun 23 '15 at 18:27

2 Answers2

2

You can use players.values().toArray to get values as array.

public Player[] getAllPlayers() {
    Player[] result = new Player[players.size()];
    return players.values().toArray(result);
}
Semyon Tikhonenko
  • 3,872
  • 6
  • 36
  • 61
0

Use the following:

public class LeagueManager {
    Map<String, Player> players = new HashMap<String, Player>();
    public void addPlayer(Player player) {
        players.put(player.getNick(), player);
    }

    public void removePlayer(Player player) {
        if (!players.isEmpty()) {
            players.remove(player.getNick());
        }
    }

    public Player getPlayer(String name) {
        if (!players.isEmpty() && players.containsKey(name)) {
            return (Player) players.get(name);
        } else {
            System.out.println("Error: there is no player with nick " + name);
            return null;
        }
    }

    public Player[] getAllPlayers() {
        if (!players.isEmpty()) {
            return players.values().toArray(new Player[players.size()]);
        } else {
            return null;
        }
    }

    public void addPoints (String name, int points) {
        if (players.containsKey(name)) {
            Player pl = (Player) players.get(name);
            pl.setPoints(points);
        }
    }
}

It returns an array of players with respect to the size of the hashMap.

Pang
  • 9,564
  • 146
  • 81
  • 122