How to join two Arrays?

Asked

Viewed 401 times

0

Hello, I have the following problem I have an Arraylist that returns several small arrays of two positions

String[] temp = new String[2];

and has the saved arrayList adds several "temp";

ArrayList lista = new ArrayList();
while(rs.next()){
   if(rs.getString("texto") != null){
    temp[0] = String.valueOf(rs.getInt("id"));
    temp[1] = rs.getString("texto");
    lista.add(temp);
   }else{
    temp[0] = String.valueOf(rs.getInt("id"));
    temp[1] = rs.getString("valor");
   }
}

Good the temp[0] contains the ID of a table my challenge and take from within the Arraylist the values and groups through the position of the ID, so would be only a few arrays like the array tempx[] contains all the temp[1] that has temp[0] with the same value. Which then goes to another Array List

Like something like that

enquanto lista.get(a).temp[0] conter o mesmo identificador faça
tempx[i] = lista.get(i).temp[1]
i++;
Quando o lista.get(a).temp[0] mudar então  um novo arrayList deve receber essa nova array.
listaAtualizada.add(tempx);

I’m not very good at ideas today and I’m struggling to do it.

1 answer

0


List tempA = new ArrayList(); for(Object l: lista){ String[] arrayA = new String[2]; String[] ArrayB = (String[]) l; int cont = 0; for(Object l2: lista){ String[] tw = (String[]) l2; if(ArrayB[0].equalsIgnoreCase(tw[0])){ tempA.add(tw[1]); } } tempX.add(tempA); tempA = new ArrayList(); } This generates many duplicated elements so I remove them using // adiciona todos os elementos incluindos duplicadas Set<String> hs = new HashSet<>(); hs.addAll(tempX); tempX.clear(); tempX.addAll(hs);

Browser other questions tagged

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