[Unsolved] ArrayList<String>..."Get" option

Discussion in 'Plugin Development' started by ASHninja1997, Aug 18, 2013.

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

    ASHninja1997

    I am trying to retrieve a players name that was saved in an ArrayList....But when ever I use
    Code:
    for(Player p : Player.get(p.getName())){


    the get is underlined red saying "

    The method get(int) in the type List<String> is not applicable for the arguments (String)

    "

    What do I do?

    Sorry my first line was small
    I typed it out here again

    Code:
    I am trying to retrieve a players name that was saved in an ArrayList...but when ever I use
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  2. Offline

    TheE

    Honestly, I have no idea what you want to do. If you want to get the first player in the array-list:
    Code:java
    1.  
    2. List<Player> players = new ArrayList<Player>();
    3. Player player = players.get(0);
    4. String name = player.getName();
    5. //do stuff
    6.  


    If you want to loop through all players in the list and do something:
    Code:java
    1.  
    2. List<Player> players = new ArrayList<Player>();
    3. for (Player player : players) {
    4. String name = player.getName();
    5. //do stuff
    6. }
    7.  


    Or do you want to get the player by its name? Performancewise this would require you to store the player in a map, using its name as key:
    Code:java
    1.  
    2. Map<String, Player> players = new Hashmap<String, Player>();
    3. Player player = players.get("PLAYERNAME");
    4. //do stuff
    5.  

    However, you should never store players in any way, store the name instead. You can then get the player directly from the server:
    Code:java
    1.  
    2. Player player = getServer().getPlayerExact("PLAYERNAME");
    3. //do stuff
    4.  
     
    ASHninja1997 likes this.
  3. Offline

    ASHninja1997

    TheE
    Thank you for your long and detailed post......I will reply if I didn't fix the problem
     
  4. Offline

    ian_x1995

Thread Status:
Not open for further replies.

Share This Page