Very odd proglem (loop)

Discussion in 'Plugin Development' started by Moon_werewolf, Jan 21, 2011.

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

    Moon_werewolf

    I have a loop that is going to remove some times but it only loops 2 times of 3 :confused:

    Here is the code. note that i have a System.out.println(i); but the only thing i get out of that is 0 and 1 not 2 :/
    Code:
    if(command[1].equalsIgnoreCase("remove"))
                    {
                        int item = stringToInt(command[2],0);
                        if(item == 0){ item = getItemId(command[2]); }
    
                        for(int i=0; i<ShopList.size(); i++)
                        {
                            ShopItem itemInShop = ShopList.get(i); System.out.println(i);
                            if(event.getPlayer().getName() == itemInShop.getUser() && (itemInShop.getType() == item || command[2].equalsIgnoreCase("all")))
                            {
                                displayMessage(event.getPlayer(),itemInShop.getAmount()+" "+itemInShop.getName()+" has been removed from the shop");
                                ShopList.remove(i);
                            }
                        }
                    }
     
  2. Offline

    xupwup

    If you remove something from that list, shoplist.size() also decreases. After deleting item i, item i+1 is moved to i, so after deleting you should decrement i, so that you look at the same index next time. If you do not do that you will skip an item.


    Code:
    if(command[1].equalsIgnoreCase("remove")) {
                        int item = stringToInt(command[2], 0);
                        if(item == 0) item = getItemId(command[2]);
    
                        for(int i=0; i<ShopList.size(); i++) {
                            ShopItem itemInShop = ShopList.get(i);
                            System.out.println(i);
                            if(event.getPlayer().getName() == itemInShop.getUser() &&
                                 (itemInShop.getType() == item || command[2].equalsIgnoreCase("all"))) {
    
                                displayMessage(event.getPlayer(), itemInShop.getAmount() +
                                    " " + itemInShop.getName() + " has been removed from the shop");
                                ShopList.remove(i);
                                i--;
                            }
                        }
                    }
     
  3. Offline

    Moon_werewolf

    Thx :D
     
Thread Status:
Not open for further replies.

Share This Page