0

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

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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);
    }
}

1 Comment

+1 The explanation is not 100% correct, but the idea and the code is good ;).
0

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

Comments

0

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
      }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.