Solved for cycle misunterstood

Discussion in 'Plugin Development' started by Ibas, Aug 7, 2013.

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

    Ibas

    Code:java
    1. for (Player players : getServer().getOnlinePlayers()) {
    2.  
    3. if(players.hasPermission("s"){
    4. players.chat("I do!");
    5. }
    6.  
    7. }



    I can't understand how does this works, could someone explain deeply? I think that this cycle runs on every prayler, which is Online at cycle start, but it isn't very informative for me :/.
     
  2. Offline

    Chiller

    Ibas Well this line: for (Player players : getServer().getOnlinePlayers()), it is saying for every player that is online, set that player to the players variable and you can then use that player in your for loop...
     
  3. Offline

    AmShaegar

    It's exactly what you said. With the permission check in addition. So basically, this is an enhanced for loop. It runs once for every Object o from all objects all.
    Code:java
    1. for(Object o : all) {
    2.  
    3. }

    all can be everything that is either an Array or implements Iterator like List, Set etc.

    The inner part contains a if statement which checks if the current player(from the current run of the for loop) has the permission s. If so, the player says "I do!" in the chat.

    Side note: The enhanced for loop has only read access to the objects o. The following will have no effect on all:
    Code:java
    1. for(Object o : all) {
    2. o = new Object();
    3. }
    4.  
     
Thread Status:
Not open for further replies.

Share This Page