Solved Use collection instead of array?

Discussion in 'Plugin Development' started by glasseater, Nov 23, 2015.

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

    glasseater

    Hey everyone,

    I'm updating a plugin I made a while back, and I am wondering how would I update Bukkit.getOnlinePlayers() to a collection.

    This is what I currently have


    Code:
    Player[] playersOnline;
                            for (int length = (playersOnline = Bukkit.getOnlinePlayers()).length, j = 0; j < length; ++j) {
                                final Player p = playersOnline[j];
    How do I change that into a collection?

    I tried .size() but that did not work.

    Thank you for your help!

    -Glass
     
  2. Offline

    Scimiguy

    Bukkit.getOnlinePlayers() is a collection.
     
  3. Offline

    Mrs. bwfctower

  4. Offline

    glasseater

    @Scimiguy
    I got an error in my ide:

    Type mismatch: cannot convert from Collection<capture#2-of ? extends Player> to Player[]
     
  5. What he's (Scimiguy) trying to say is, getOnlinePlayers() returns a Collection<? extends Player>.
    Your question might have been incorrectly worded, do you want an array from a Collection or a Collection from an array?
    There'd be no point to get a Collection from the array because getOnlinePlayers() already returns a Collection. However, an array from a Collection can be done by using the method Collection#toArray().
    E.g. Player[] onlinePlayers = Bukkit.getOnlinePlayers().toArray(new Player[Bukkit.getOnlinePlayers().size()]);

    However, from what you're doing, it seems you could just do a foreach loop.
    E.g.
    Code:
    for (Player onlinePlayer : Bukkit.getOnlinePlayers()) { // Iterate through each player in the Collection.
    
    }
    
     
  6. Offline

    glasseater

    @KingFaris11
    Cool, thank you! Would I be able to still use this:
    Code:
    j = 0; j < length; ++j
    with .size? I tried but it didn't seem to work...
     
  7. @glasseater
    Only something similar to that is possible with lists, because they have a get(index) method that allows you to retrieve an element at a certain index.
     
  8. Offline

    Scimiguy

    @glasseater
    You wouldnt' need to use that if you'd use the example KingFaris gave you
     
  9. Offline

    glasseater

    @Scimiguy
    But then I cant define p?
    final Player p = playersOnline[j];
     
  10. Offline

    Scimiguy

    Youre defining p every loop

    For (Player p : Bukkit.getOnlinePlayers())

    Thats called a for each loop, or an extended for loop.

    What it does is loops over each element in collection, handing it to you one at a time.
     
  11. Offline

    glasseater

  12. Offline

    Scimiguy

    @glasseater
    Please edit the title of this thread and mark it as solved
     
  13. Offline

    TheNewTao

    Question, what is the difference between a collection and an ArrayList?
     
  14. Offline

    Mrs. bwfctower

    @TheNewTao An ArrayList is a Collection. A Collection is an interface found in the java.util package shipped since JDK 1.2. It is used in many of the ADTs (Abstract Data Types) found in the java.util package, including Lists, Sets, Queues, and Deques.
     
Thread Status:
Not open for further replies.

Share This Page