Solved Get player name from arraylist

Discussion in 'Plugin Development' started by deleted_90940145, May 2, 2014.

Thread Status:
Not open for further replies.
  1. Hi,

    I have this arraylist:
    Code:java
    1. public static ArrayList<Player> players = new ArrayList<Player>();

    And I want to get the name of the final player from that arraylist, I already know how to get the final player, but not the name, this is what I have at the moment:
    Code:java
    1. String finalplayer = players.toString();

    But it returns [CraftPlayer{name=PlayerName}], how would I get the playername only?

    Thanks.
     
  2. Offline

    DxDy

    Code:java
    1. Player finalplayer = players.get(players.size() - 1);
    2.  
    3. String finalPlayerName = finalplayer.getName();
     
  3. Offline

    Superckl1

    driver-e

    That's not how you get the last element in a list...

    Do players.get(players.size()-1); then call get name on the returned Player.

    Also, it is strongly discouraged to keep Collections of Players. Keep their names instead. What you have can lead to memory leaks.
     
    hintss likes this.
  4. Offline

    DxDy

    players.get(players.size()); will produce an ArrayIndexOutOfBounds-Exception.
     
  5. Offline

    Superckl1

    DxDy
    Indeed, I had already edited my post by the time you posted this :p
     
    DxDy likes this.
  6. DxDy Thank you!
    Superckl1 Yea, I said I already know about how to get the last player, but thanks :p
     
Thread Status:
Not open for further replies.

Share This Page