Java Array List: How to show a Jlist the values and Positions of the String Array

Asked

Viewed 323 times

0

I made this code in 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?

  • Qjual the problem that occurs?

1 answer

1


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

-your "text =" it goes on writing every time you pass only replace by "+="
but I recommend you use a Stringbuilder ;)
hope I’ve helped

Browser other questions tagged

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