Question

In this code i need an object to be removed if a certain value is zero [Portfolio.total.get(k).get(c).current_quantity==0].

for(int k=0;k<Run_process.total_Agents;k++){
        for(int c=0;c<Portfolio.total.get(k).size();c++){
            if(Portfolio.total.get(k).get(c).current_quantity==0){
                System.out.println("delete Agent"+k+" "+Portfolio.total.get(k).get(c).stocks.stock_Id);
                Portfolio.total.get(k).remove(c);
                //remove from portfolio if there is no quantity
            }
        }
        //Portfolio.total.get(k).trimToSize();
    }
    //Portfolio.total.trimToSize();
    for(int k=0;k<Run_process.total_Agents;k++){
        for(int c=0;c<Portfolio.total.get(k).size();c++){
            if(Portfolio.total.get(k).get(c).current_quantity==0){
                System.out.println("still zero quantity Agent"+k+" "+Portfolio.total.get(k).get(c).stocks.stock_Id);
                //remove from portfolio if there is no quantity
            }
        }
        //Portfolio.total.get(k).trimToSize();
    }

The problem is that after i run this loop again to check if everything is ok BUT sometimes seems that 1-3 values although have [Portfolio.total.get(k).get(c).current_quantity==0] are still in the arraylist.The next time this code runs this object is properly deleted. To sum up some values are deleted the next time this code runs

Was it helpful?

Solution

When you remove something, all following indexes decrease by 1. Incrementing your loop counter will skip the next element. They are the ones you are missing.

You need to do something like this:

Portfolio.total.get(k).remove(c); // you already have this line
c--;

Also, for the sake of readability I would prefer to use an Iterator and it's remove() method. This way you don't have to worry about indexes.

OTHER TIPS

you should not delete items from an array while iterating over it forward. The Problem is probably because of two items with quantity directly one after another

e.g. item[0].current_quantity is 0 and item[1].current_quantity is 0

So if you iterate over your list, item[0] will be removed, item[1] will become item[0]. Now you increment your index, and skip your former item[1]. That is why it is still in your list.

If you really want to iterate over you list and delete items while iterating, you should do that backwards:

for(int c = Portfolio.total.get(k).size() - 1; c > 0;c--) {
    if(Portfolio.total.get(k).get(c).current_quantity==0) {
        Portfolio.total.get(k).remove(c);
    }
}

this is because you are skipping many items! when you remove an object, the list become smaller, so you are not checking the object that comes after this, every time after you remove an object, do a c--, it will solve the problem

You are accessing a list and inside the for loop removing an item from the same list. This can be problematic in many ways. You should better use Iterator for this:

 for(Iterator<YourObj> it=Portfolio.total.get(k).iterator(); it.hasNext();) {
      YourObj obj = it.next();
      if(obj.current_quantity==0){
            System.out.println("delete Agent"+k+" "+obj.stocks.stock_Id);
            it.remove();
            //remove from portfolio if there is no quantity
      }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top