简体   繁体   English

在 Java 中打印 ArrayList 时出现问题

[英]Problems printing ArrayList in Java

I'm having to make a phonebook for a exercise and I need to do it using ArrayList, I added the option to register users in the menu, but when printing these users, it gives me something like: [Contact@68e09d7b, Contact@51da2082]我必须制作电话簿进行练习,我需要使用 ArrayList 来完成,我在菜单中添加了注册用户的选项,但是在打印这些用户时,它给了我类似的信息: [Contact@68e09d7b, Contact@51da2082]

I researched here on Stack Overflow and saw people who had problems similar to me, and in the answers, people told them to use the Override toString() method in the main class, but when I tried to do that, my code returned both lists within a list.我在 Stack Overflow 上进行了研究,看到了和我有类似问题的人,在答案中,人们告诉他们在主 class 中使用Override toString() 方法,但是当我尝试这样做时,我的代码返回了两个列表在一个列表中。

Output: Output:

[Jon, 99999-9999, jon@gmail.com, 01/01/2000, Tom, 99999-9998, tom@gmail.com, 02/01/2000]

Expected:预期的:

[Jon, 99999-9999, jon@gmail.com, 01/01/2000]
[Tom, 99999-9998, tom@gmail.com, 02/01/2000]

Here are the two classes I used to make the program.这是我用来制作程序的两个类。

Contact.class联系方式.class

public class Contato {
  // Atributos da classe Contato
  private String id;
  private String nome;
  private String telefone;
  private String email;  
  private String aniversario;
  
  public String getID() {
    return id;
  }
  public void setID(String id) {
    this.id = id;
  }
  
  public String getNome() {
    return nome;
  }
  public void setNome(String nome) {
    this.nome = nome;
  }
  
  public String getTelefone() {
    return telefone;
  }
  public void setTelefone(String telefone) {
    this.telefone = telefone;
  }
  
  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }

  public String getAniversario() {
    return aniversario;
  }
  public void setAniversario(String aniversario) {
    this.aniversario = aniversario;
  }
  
  public Contato(String nome, String telefone) {
    this.id = id;
    this.nome = nome;
    this.telefone = telefone;
    this.email = "Email not entered";
    this.aniversario = "Birthday date not entered";
  }
  
   @Override
   public String toString() {
     return nome + ", " + telefone + ", " + email + ", " + aniversario;
  }
}

Main.class主.class

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
 
public class Main{
  public static void main(String args[]){
    Scanner entrada = new Scanner(System.in);
    String id, nome, telefone, email, aniversario;
    ArrayList<Contato> contatos = new ArrayList<>();
     
    Agenda agenda = new Agenda();
     
    while(true){
      System.out.println("\n1. Register New Contact");
      System.out.println("2. Search Contact");
      System.out.println("3. Delete Contact");
      System.out.println("4. Exit");
      System.out.print("Your choice: ");
      int opcao = Integer.parseInt(entrada.nextLine());
       
      switch(opcao){
        case 1:
          System.out.print("Name: ");
          nome = entrada.nextLine();
          System.out.print("Phone number: ");
          telefone = entrada.nextLine();
          Contato novoContato = new Contato(nome, telefone);
          System.out.print("E-mail: ");
          email = entrada.nextLine();
          novoContato.setEmail(email);
          System.out.print("Birthday date: ");
          aniversario = entrada.nextLine();
          novoContato.setAniversario(aniversario);

          contatos.add(novoContato);
          break;
        case 2:
          System.out.println(contatos);
          break;
      }
    }
  }
} 

Instead of trying to print the list, print each contact in turn separately不要尝试打印列表,而是分别依次打印每个联系人

for (Contato contato : contatos) {
    System.out.println("[" + contato + "]");
}

You could also make use of something like StringJoiner , for example...你也可以使用类似StringJoiner的东西,例如......

StringJoiner sj = new StringJoiner("\n");
for (Contato contato : contatos) {
    sj.add(contato.toString());
}
System.out.println(sj);

Although, for performance, I'd probably stick to just looping over the list虽然,为了性能,我可能会坚持只循环列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM