Java Array List: How to show a jlist rm the String Array values and Positions

0

I made this code in the class:

public class Dados {
ArrayList<Contato> contatos = new ArrayList();

public void cadastraContato(Contato contato){
    contatos.add(contato);
}
public String  numeroDeContatos(){
        return contatos.size()+ "";
}
public String listarContatos(){
    int n = contatos.size();
    int r;
    String text = null;
    for(int i =0; i<=n; i++){
         r = Integer.parseInt(contatos.get(i).toString());
        text = contatos.get(i).toString();
    }
    return text;
}

}

Code in the Jframe of the add button event:

    private void btCadastrarActionPerformed(java.awt.event.ActionEvent evt) {                                            
    String nome = tfNome.getText();
    String email = tfEmail.getText();
    String cpf = tfCpf.getText();

    Contato c = new Contato(nome, email, cpf);

    dados.cadastraContato(c);
    lbTotal.setText(dados.numeroDeContatos()+ " Contatos cadastrados");
    tfNome.setText("");
    tfEmail.setText("");
    tfCpf.setText("");
    JOptionPane.showMessageDialog(this, "Contato cadastrado com sucesso!");
    System.out.println(c);

    int index = Integer.valueOf(dados.numeroDeContatos()) -1;
    lstContato.setModel(model);
    model.addElement(index + dados.listarContatos());
}

What's wrong? and what can I do to find the solution?

    
asked by anonymous 11.03.2017 / 05:40

1 answer

1
for(int i =0; i<=n; i++){
         r = Integer.parseInt(contatos.get(i).toString());
        text = contatos.get(i).toString();
    }

-seu "text=" it goes on writing every time you pass, just replace with "+="
but I recommend you use a StringBuilder;)
I hope I have helped

    
23.04.2017 / 04:09