Keyset to Player[]

Discussion in 'Plugin Development' started by Aza24, Feb 7, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Aza24

    Hey, I am looking for the most efficient way to convert a Keyset in an hashmap which contains players names into Player[]
     
  2. Code:
    Player[] players = new Player[hashmap.keySet().size()];
    for(int i = 0; i < hashmap.keySet().size(); i++)
    {
        players[i] = Bukkit.getServer.getPlayer(hashmap.keySet().get(i));
    }
     
  3. Offline

    Aza24

    Ok, pretty smiler than what I did. Thanks anyway.
     
  4. Player[] players = hashmap.keySet().toArray(new Player[0]);

    I won
     
  5. Offline

    Njol

    You lost to an ArrayStoreException because String can't be cast to Player.
     
  6. Offline

    Aza24

    This is what I did, anything wrong?

    Code:Java
    1. public Player[] getWinningPlayers() throws NullPointerException {
    2. if(winningPlayers == null) throw new NullPointerException();
    3.  
    4. int numPlayers = winningPlayers.keySet().size();
    5. Player[] output = new Player[numPlayers];
    6.  
    7. for(int i=0; i<numPlayers; i++) {
    8. output = Bukkit.getServer().getPlayer((String)winningPlayers.keySet().toArray());
    9. }
    10.  
    11. return output;
    12. }
     
  7. Offline

    Njol

    Code:Java
    1. for(int i=0; i<numPlayers; i++) {
    2. output[i] = Bukkit.getServer().getPlayer((String)winningPlayers.keySet().toArray()[i]);
    3. }[/i][/i]
     
  8. Offline

    Aza24

    Woops, didn't see that. Thanks!
     
  9. a little suggestion: I wouldn't throw a nullpointerexception. I would just return an empty array.
     
  10. Offline

    Aza24

    I did it because its for an API
     
  11. I asumed it was an Set<Player> instead of Set<String>
     
Thread Status:
Not open for further replies.

Share This Page