Run 2 for loops after another

advertisements

I want to run 2 loops after each other, but it doesn't give me the second log message which is p.sendMessage() because it stops by the 1st loop. I have already tried some things like different methods, but I need the lists. Can anybody help me with this?

if(args[0].equalsIgnoreCase("start")){
    if(game.players.containsKey(p.getUniqueId())){
        String arenaname = game.players.get(p.getUniqueId());
        ArrayList<UUID> listofplayer = new ArrayList<UUID>();
        for (Entry<UUID, String> entry : game.players.entrySet()) {
            if(entry.getValue().equalsIgnoreCase(arenaname)){
                listofplayer.add(entry.getKey());
                p.sendMessage("added");
            }
        }
        for(int i=0; i==listofplayer.size()+1; i++){
            UUID uuid = listofplayer.get(i);
            p.sendMessage("added2");
            for(Player player : Bukkit.getServer().getOnlinePlayers()){
                if(player.getUniqueId().equals(uuid)){
                    p.sendMessage("added3");
                    World world = Bukkit.getWorld(main.arenas.getString("arenas." + arenaname + "." + i+1 + ".world"));

                    Location loc = new Location(world, main.arenas.getDouble("arenas." + arenaname + "." + i+1 + ".X")
                            , main.arenas.getDouble("arenas." + arenaname + "." + i+1 + ".Y")
                            , main.arenas.getDouble("arenas." + arenaname + "." + i+1 + ".Z"));
                    player.teleport(loc);
                }
            }
        }
    }
}


It never enters the second loop. You have i==listofplayer.size()+1 as the condition. I doubt that is what you meant, and since you set i=0, this will never be true. You probably want i < listofplayer.size(). That will allow your loop to loop through each player in listofplayer.

for(int i=0; i < listofplayer.size(); i++){

Notice that I removed the +1 at the end of listofplayer.size() as well. This is because including that would lead to an IndexOutOfBounds exception, because the last iteration of the loop would attempt to access an index in the array that doesn't exist. Arrays are 0 indexed, so the last index is always 1 less than the length of the array.