Remove subArray from a Main Array

Asked

Viewed 65 times

1

I have two main Arrays (listaInformacoesNota,listaInformacoesPedidosPostgreSQL) with N subArrays each, these subArrays have items in common and a different item only, I did the following to compare these each subArray:

for (List conteudoNotas : listaInformacoesNota){
    for (List conteudoPedidos : listaInformacoesPedidoPostgreSQL){
        if (conteudoNotas.get(3).toString().equals(conteudoPedidos.get(2).toString()) && conteudoNotas.get(4).toString().equals(conteudoPedidos.get(4).toString())){
            if (conteudoNotas.get(1).toString().equals(conteudoPedidos.get(1).toString()) && conteudoNotas.get(2).toString().equals(conteudoPedidos.get(3).toString())){
             \\Aqui eu precisaria remover o subArray (conteudoNotas) do Array principal (listaInformacoesNota)
            }
        }
    }
}

My need is to remove from the Main Array listaInformacoesNota the subArray condicoesNotas that fit the conditions, so that it is not compared again with any other subArray.

  • What is the content of the 2 arrays? Why access to specified arrays positions within ifs (conteudoNotas.get(3)...) ?

  • What exactly is your question? To remove an item from a list just do lista.remove(item), but you cannot do this while iterating the list in question. To do this you need to use the interface Iterator. You can have the required iterator through listaInformacoesNota.iterator();. Through the Iterator you will be able to iterate the list (actually iterate the Iterator) and remove the desired item.

1 answer

3


To remove an item from the list, in the middle of the iteration of the list, Iterator should be used.

Iterator<List> it = listaInformacoesNota.iterator();
while (it.hasNext()) {
    List conteudoNotas = it.next();
    for (List conteudoPedidos : listaInformacoesPedidoPostgreSQL) {
        if (conteudoNotas.get(3).toString().equals(conteudoPedidos.get(2).toString()) && conteudoNotas.get(4).toString().equals(conteudoPedidos.get(4).toString())) {
            if (conteudoNotas.get(1).toString().equals(conteudoPedidos.get(1).toString()) && conteudoNotas.get(2).toString().equals(conteudoPedidos.get(3).toString())) {
                it.remove();
            }
        }
    }
}

Note: I strongly recommend that you create classes for Informacaonota and Informacaopedido and use object lists of these classes instead of list lists. In these classes you would put attributes with meaningful names to store what these get(1), get(2), get(3)...

For example:

if (conteudoNota.getOrigem().equals(informacaoPedido.getDestino()) && conteudoNota.getCodigo().equals(informacaoPedido.getCodigo())...

Browser other questions tagged

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