ArrayList - Place index next to element

-2

I have an application where the user informs via JSF a drive name.

When the system finds the drive, it fills in List<String> with the drive data. With this List filled, I need to put the index of each line next to its element:

[0] - elemento da linha aqui
[1] - elemento da linha aqui 1
[2] - elemento da linha aqui 2
[3] - etc

I can print to the console, but when I send it to JSF, the data is overwritten.

My bean looks like this:

public List<String> getUnidadesAdicionadas() {
List<String> lista = new ArrayList<String>();
    try {

            if (!findUnidadeCampanha(unidade)) {
                addMensagemErro("erro");
            }  else { 
            unidadesAdd.add(unidade.getNome());
            quantidadeUnidades = unidadesAdd.size();


            lista.addAll(unidadesAdd);

            for (int i = 1; i < unidadesAdd.size(); i++) {   
                //Para cada item percorrido   atribuir o valor na variavel indice;
                String elemento  = unidadesAdd.get(i) + "\n";
                 int  index  = i++;
                }

            unidade.setNome("");//limpar campo UNIDADE


        } 
    } catch (Exception e) {
        e.printStackTrace();
    }
    return lista;
}

and my jsf like this:

 <h:outputText value="#{proformeGerencialMBean.index}"  escape="false" />   - <h:outputText value="#{proformeGerencialMBean.elemento}"  escape="false" />
    
asked by anonymous 05.09.2018 / 18:38

3 answers

0

On the face of it you see that you are returning the same list instead of drivesAdd which is the one you are fiddling with, would not it be returning drivesAdd?

List<String> lista = new ArrayList<String>();

This list you created always will never have the change because you are only adding the element to it.

    
06.09.2018 / 22:51
0

Try changing your for to:

for (int i = 1; i < lista.size(); i++) {   
    //Para cada item percorrido   atribuir o valor na variavel indice;
    String elemento  = lista.get(i) + "\n";
    int  index  = i++;
}
    
06.09.2018 / 22:53
0

The solution would be more or less that maybe you have to create another list and add the elements in it, you have to test to see.

Integer contador = 0;

for (String elemento : lista) {   
    //Para cada item percorrido atribuir o valor na variavel indice;
    elemento = "[" + contador + "]" + elemento;
    contador++;
}
    
06.09.2018 / 22:58