How to override the remove() method from Iterator?

Asked

Viewed 234 times

3

I created a repository (RepositorioObjeto) and it implements the interface Iterator on the object the repository stores (Iterator<Objeto>). So I had to override the methods next(), hasNext() and remove(). I had to adapt the next(), because it only returns the object (that is, it does not increment it, I left it to another method because it had some methods that needed to give in the same two iteration next(), causing it to move to another object). It follows the code of next() and hasNext():

public Vendedor next(){
    Vendedor vendedor = this.vendedores.get(posicao);
    return vendedor;
}

public boolean hasNext(){
    boolean temProximo = true;//posto true pois comumente será mais vezes true
    if (posicao >= this.vendedores.size() || this.vendedores.get(posicao) == null) {
        temProximo = false;
        zerarContadorPosicao();
    }
    return temProximo;
}

public static void incrementarContadorPosicao(){
    posicao++;
}

OBS.: position is a static attribute

The big problem I don’t know how it is about remove(), what should be implemented? For the removal method [removerVendedor] of the object (which is of the type Vendedor), I want you to use the remove() that comes with the Iterator, so much that I call in the method removerVendedor, but for this has to be implemented, but how, without there being code redundancy (code repetition)?

public void removerVendedor(String cpfVendedor) throws NaoEncontradoVendedorException{
    boolean removeu = false;
    if (!cpfVendedor.equals("") && !cpfVendedor.equals(" ")){
        ListIterator<Vendedor> iVendedor = this.vendedores.listIterator();
        while(iVendedor.hasNext()){
            if (iVendedor.next().getCpf().equals(cpfVendedor)){
                iVendedor.remove();
                removeu = true;
                zerarContadorPosicao();
                salvarArquivo();
                break;
            }
            incrementarContadorPosicao();
        }
    }

    if (removeu == false){
        throw new NaoEncontradoVendedorException();
    }
}

@Override
public void remove() {  
}
  • It is strange that the next one returns the current one. Perhaps there is a problem with the logic of it. It has how to clarify this part?

  • You will have difficulties while not solving these problems: to) You should not use a static variable (position) to store the state of the object as it does not belong to any object. b) The next of Iterator should navigate to the next item and then return it, this is the Iterator convention. c) Every time Iterator is requested, a new instance of it should be created, so you will not have problems with the same list being run by two different consumers.

  • 1

    As to the remove, your existing method removes by CPF, nothing to do with the remove iterator, which removes the item from the current position. No remove iterator you can simply call the remove of the list passing the current index. Remember to validate the current position and change it to the previous item after removal (see @Reginaldosoares' reply). If you end up with redundant code in both methods, move the repeated part of the code to a new method in order to reuse it both on remove iterator as in the method that removes by Cpf.

  • @Caffé my repository implements Iterator. My basic class is Seller. And I created a repository called Repositorioseller and implemented Iterator<Seller>. The position attribute is a static attribute of the Repositoriovendedor class. What did you say is still valid (as for Static)? Another thing I didn’t understand was that it is stated that next should go to the next and return it, but isn’t that what I’m doing? And finally, you’re saying that every time I need an iterator twice, I should create two instances of it? How to control that he doesn’t compare him to himself?

  • @Jnmarcos Yes, variable Static is still not suitable to hold the current position. No, that’s not what you’re doing - you wrote yourself that it does not increase the next, instead returns the current one. Yes, if you need two Iterators, should create two instances (should create a new instance each time a iterator requested). I did not understand the last question. Look for examples of implementing Iterators. It’s quite simple - keep it simple. If you want, post the rest of your relevant code so we can help.

1 answer

1

In my view it would be:

@Override
public void remove() {  
      if(posicao <= 0) {
            throw new IllegalStateException("elementos indisponiveis!");
        }
        this.vendedores.remove(--posicao);
}
  • I don’t understand why this --position. It shouldn’t just be a position, since I increase it later?

  • @Jnmarcos by the implementation reference of his code inferred that "position" would never be 0 since: position >= this.vendedores.size() ie position != index, probably started at 1. Therefore the position test <= 0 in override in addition to the decrement previously removing the element from the list by index.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.