PAGUE 6 MESES
LEVE 12 MESES
GARANTIR DESCONTO

Fórum Comparando ArrayLists de ArraList<Integer> #498566

21/10/2014

0

Bom, tenho um ArrayList<ArrayList<Integer>> chamado listaRepetida.

E um determinado momento ela retorna 2 ArrayList<Integer>.da forma abaixo:
[0-> [1,3,3,2], 1->[3,1,1,4] ]

Mas essa ordem de 4 inteiros pode ser mudada para um índice com 4 inteiros, outro índice com 2 inteiros, semelhante ao EXEMPLO abaixo:
[0-> [1,3,3,2], 1->[2,1], 2->[1,2,3,4,5,6] ]

Mas sempre em quantidades pares. Ou seja, ou o ArrayList tem 2 ou 4 ou 6 ....... números inteiros.

O que eu preciso saber é como comparar cada dois números de cada ArrayList.

Por exemplo:
[0-> [1,3,3,2], 1->[3,1,1,4] ]

comparações:
1,3 do índice 0 com 3,1 índice 1
1,3 do índice 0 com 1,4 índice 1

Depois
3,2 do índice 0 com 3,1 índice 1
3,2 do índice 0 com 3,1 índice 1

As comparações levam em conta que 3,1 é igual a 1,3 e analogamente 3,2 é igual a 2,3 e assim por diante..

De forma a no final, ter uma String texto de retorno com o resultado das comparações.

Nesse caso encontramos que
1,3 do índice 0 é igual a 3,1 índice 1

Então, só entra 1.

Então a variável texto seria:
{1-3, 3-2, 1-4}

Note que o 3-1 foi retirado.

Tentei da forma abaixo mas não deu certo.
Minha lógica não esta adequada.
import java.util.ArrayList;

public class RemoveRepetidas
{
  ArrayList<ArrayList<Integer>> listaRepetida = new ArrayList<ArrayList<Integer>>();
  
  RemoveRepetidas (ArrayList<ArrayList<Integer>> _listaRepetida)
  {
    this.listaRepetida = _listaRepetida;	
  }
  
    	
  String removeRepetida()
  {
	       String texto = "{";
	
		   for (int i = 0; i < listaRepetida.get(i).size(); i++)
		   {				 
  		      for (int j = i+2; j < listaRepetida.get(i+2).size(); j++)
		      {
				 if (
				       ( listaRepetida.get(i).get(0) == listaRepetida.get(j).get(0) && listaRepetida.get(i).get(1) == listaRepetida.get(j).get(1) ) ||
					   ( listaRepetida.get(i).get(0) == listaRepetida.get(j).get(1) && listaRepetida.get(i).get(1) == listaRepetida.get(j).get(0) )
					)
					{
					   System.out.println(listaRepetida.get(j).get(0));
					   System.out.println(listaRepetida.get(j).get(1));
				       System.out.println();
					}
					else
					{
					   texto +=listaRepetida.get(i).get(0)+"-"+listaRepetida.get(i).get(1);
					}
				j+=2;
			  }
			  i+=2;
		   } 
		   
           texto += "}";
	
		   System.out.println(texto);
		   
		   return texto;
  }
}

O que fazer?
Carlos Rocha

Carlos Rocha

Responder

Posts

21/10/2014

Carlos Rocha

Veja só:
for (int i = 0; i < conta.size(); i++)
{
listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
i+=2;
}

Isso esta me retornando um ArrayList de inteiros assim ( no momento)
[1,0,1]

Isso me diz que a cada sequencia de 3 números tenho o primeiro.
1, que preciso pegar outro ArrayList<ArrayList<Integer>> chamado listaRepetida e remover do seu indice 1 (conta.get(i)) seus índices 0 e 1 (conta.get(i+1) e conta.get(i+2)).

Mas não esta removendo.
Tanto antes quanto depois da remoção this.listaRepetida continua:
[0-> [1,3,3,2], 1->[3,1,1,4] ]

O correto deveria ser:
[0-> [1,3,3,2], 1->[1,4] ]

Onde esta o erro?
Veja toda a classe
import java.util.ArrayList;

public class RemoveRepetidas
{
  ArrayList<ArrayList<Integer>> listaRepetida = new ArrayList<ArrayList<Integer>>();

  RemoveRepetidas (ArrayList<ArrayList<Integer>> _listaRepetida)
  {
    this.listaRepetida = _listaRepetida;
  }

    
  String removeRepetida()
  {      
     System.out.println(this.listaRepetida);
     ArrayList<Integer> conta = new ArrayList<Integer>();
    ArrayList<ArrayList<Integer>> listaRepetida2 = listaRepetida;


     for (int i = 0; i < listaRepetida2.size(); i++)
     {
        for (int j = i+1; j < listaRepetida2.size(); j++)
        {
             for (int k = 0; k < listaRepetida2.get(i).size(); k++)
             {
                 for (int l = 0; l < listaRepetida2.get(j).size(); l++)
                 {
                     if (
                          ( listaRepetida2.get(i).get(k) == listaRepetida2.get(j).get(l) && listaRepetida2.get(i).get(k+1) == listaRepetida2.get(j).get(l+1) ) ||
                          ( listaRepetida2.get(i).get(k+1) == listaRepetida2.get(j).get(l) && listaRepetida2.get(i).get(k) == listaRepetida2.get(j).get(l+1) )
                        )
                        {
                            conta.add(j);
                            conta.add(l);
                            conta.add(l+1);
                        }

                        l++;
                 }

                 k++;
             }
        }
      }

      for (int i = 0; i < conta.size(); i++)
      {
        listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
        listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
        i+=2;
      }

     String texto = "{";
     texto += "}";

     System.out.println(listaRepetida2);

     return texto;
  }
}
Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar