0

I don't know how to remove an object from an ArrayList. I know there is remove but in my case, its more complicate.

I have a ArrayList (listsBlocksRemaining), which contains object that I put into another ArrayList (listBlocksIntoRibbon) when there are print. Then, what I want, its that the object that are print should be removed from listBlocksRemaining, there are print so no more remaining.

This is my code :

public void algoGlouton(Graphics g) {

    int offsetX = 13;
    int offsetY = 600;
    int widthAvailable= 0;
    int smallestHeight= 300;
    for(Bloc b : listsBlocksRemaining) {
        if(widthRemaining+b.getWidth () < ribbon.getWidth ()) {
            widthAvailable+= b.getWidth();
             g.setColor(b.getColor());
             g.fillRect(offsetX, offsetY-b.getHeight(), b.getWidth(), b.getHauteur());
             listBlocksIntoRibbon.add(b);
             b.setX(offsetX);
             b.setY(offsetY-b.getHauteur());
             offsetX += b.getLargeur();
        } else {
            for(Bloc b1 : listBlocksIntoRibbon) {
                if(b1.getHauteur() < smallestHeight) {
                    smallestHeight= b1.getHeight();
                }
            }
        }
    }
}

I tried this :

for(int i = 0; i<listsBlocksRemaining.size();i++) {
        for(int j = 0; j<listBlocksIntoRibbon.size();j++) {
            if(listsBlocksRemaining.get(i) == listBlocksIntoRibbon.get(j)) {
                listsBlocksRemaining.remove(i);
            }
        }
}

and this :

for(int i = 0; i<listsBlocksRemaining.size();i++) {
        Bloc b = listsBlocksRemaining.get(i);
        if(listBlocksIntoRibbon.equals(b)) {
            listsBlocksRemaining.remove(b);
        }
}

Cordially

4
  • I prefer to override equals() and hashCode() methods in Bloc and then just call listsBlocksRemaining.remove(b) Commented Dec 13, 2018 at 17:28
  • @istovatis what do you mean ? Commented Dec 13, 2018 at 17:28
  • If I understand correctly, you want to remove all the elements of listBlocksIntoRibbon from listsBlocksRemaining, right? listsBlocksRemaining.removeAll(listBlocksIntoRibbon) does that. docs.oracle.com/javase/8/docs/api/java/util/… Commented Dec 13, 2018 at 17:28
  • First of all thanks for your help, but what I want is that when a block has been placed into the listBlocksIntoRibbon I instantly delete it from listBlocksRemaining, with what you said, it'll cause an Exception. Commented Dec 13, 2018 at 17:40

2 Answers 2

1

You don't need to find the object in listBlocksRemaining in order to remove it. You can just remove the object from the list as:

for( Bloc bloc : listBlocksIntoRibbon )
  listBlocksRemaining.remove(bloc);

or even:

listBlocksRemaining.removeAll( listBlocksIntoRibbon );
Sign up to request clarification or add additional context in comments.

5 Comments

First of all thanks for your help, but what I want is that when a block has been placed into the listBlocksIntoRibbon I instantly delete it from listBlocksRemaining, with what you said, it'll cause an Exception.
Then right after you do listBlocksIntoRibbon.add(b);, just do listBlocksRemaining.remove(b);, but you should loop through listsBlocksRemaining with an iterator and not a for loop.
If you want to instantly remove it from the list you are iterating over, you need to use an actual Iterator, and call remove() on the iterator after you add it to listBlocksIntoRibbon.
Yes, I've just see someone put an anwser with a Iterator, but there is still a problem. Thanks :)
Yes, its what I did, but i've still an error.. could I give you my project (eclipse) and check why its not doint it correctly please ?
0

To safely remove from a collection while iterating over it you should use an Iterator.

Try this:

 Iterator<Bloc> itr = listsBlocksRemaining.iterator();
 while(itr.hasNext()){
      Bloc b = itr.next();
      ...
       listBlocksIntoRibbon.add(b);
       itr.remove();
       ...
 }

1 Comment

My bad, I import the wrong thing.. thanks, i'll try now

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.