在 java 中显示整个数组列表

display an entire arraylist in java

我对 java 还是很陌生。我正在尝试制作一个基本上将联系人添加到数组列表的程序。就创建新 object 和设置 name/number 而言,我已经弄清楚了一切。据我所知,它正在将它添加到数组中,但是我不确定如何显示数组?我想添加一段代码,在您添加每个联系人后显示数组列表。

这是我的联系人 class,不确定我是否需要 PhoneBook 方法用于数组....

public class Contact {
  String first; //first name
  String last; //last name
  String phone; //phone number
  String PhoneBook; //array list???

  public void PhoneBook(String f, String l, String p) {
    first = f;
    last = l;
    phone = p;

  }

  public void setFirst(String first) {
    this.first = first;
  }

  public void setLast(String last) {
    this.last = last;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  public Contact makeCopy() {
      Contact Contact = new Contact();
      Contact.first = this.first;
      Contact.last = this.last;
      Contact.phone = this.phone;
      return Contact;
    } //end makeCopy
} //end class Computer

这是我的 driver class...

import java.util.Scanner;
import java.util.ArrayList;
public class contactDriver {
  public static void main(String[] args) {
      Contact Contact = new Contact(); //make default Contact
      Contact newContact;

      String first; //first name
      String last; //last name
      String phone; //phone number
      String input; //answer to create a new contact

      boolean add = true; //boolean to add new contact

      Scanner scan = new Scanner(System.in);

      Contact.setFirst("Default");
      Contact.setLast("Default");
      Contact.setPhone("Default");

      while (add) {
        System.out.println("Would you like to create a new contact? (Y/N)");
        input = scan.nextLine();
        if (input.equals("Y") || input.equals("y")) {
          newContact = Contact.makeCopy();

          System.out.println("Enter the contact's first name: ");
          first = scan.nextLine();

          System.out.println("Enter the contact's last name: ");
          last = scan.nextLine();

          System.out.println("Enter the contact's phone number: ");
          phone = scan.nextLine();

          ArrayList < Contact > PhoneBook = new ArrayList();
          newContact.setFirst(first);
          newContact.setLast(last);
          newContact.setPhone(phone);
          PhoneBook.add(newContact);
        } else {
          add = false;
          System.out.println("Goodbye!");
          break;
        }
      }
    } //end main
} //end Class ComputerDriver

如果只是为了打印,覆盖 Contact class 的 toString 方法,就像:

@Override
public String toString() {
    return first + " " + last + "; phone number: " + phone;
}

然后,在您的 main 方法中,打印所有联系人:

for (Contact c : phoneBook) {
    System.out.println(c);
}

此外,您应该在循环之外创建电话簿,ArrayList

您的 Contact class 应定义为:

public class Contact {
    private String first; // first name
    private String last; // last name
    private String phone; // phone number

    public Contact(String f, String l, String p) {
        first = f;
        last = l;
        phone = p;
    }

    public String getFirst() {
        return first;
    }

    public String getLast() {
        return last;
    }

    public String getPhone() {
        return phone;
    }

    public Contact makeCopy() {
        return new Contact(first, last, phone);
    }
    @Override
    public String toString() {
        return first + " " + last + "; phone number: " + phone;
    }
}

你的 main 方法应该是:

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    List<Contact> phoneBook = new ArrayList<>();

    while (true) {
        System.out.println("Would you like to create a new contact? (Y/N)");
        String input = scan.nextLine();
        if (input.equalsIgnoreCase("Y")) {
            System.out.println("Enter the contact's first name: ");
            String first = scan.nextLine();

            System.out.println("Enter the contact's last name: ");
            String last = scan.nextLine();

            System.out.println("Enter the contact's phone number: ");
            String phone = scan.nextLine();

            Contact contact = new Contact(first, last, phone);
            phoneBook.add(contact);
            for (Contact c : phoneBook) {
                System.out.println(c);
            }
        } else {
            System.out.println("Goodbye!");
            break;
        }
    }

    scan.close();
}

编译器会给出警告,很有可能是因为:

String PhoneBook;

当你知道你也有

public void PhoneBook(String f, String l, String p)

还有更多电话簿

ArrayList < Contact > PhoneBook = new ArrayList();

尝试使用另一个变量名和函数名是安全的,并确保它们是不同的,尤其是

String PhoneBook;
public void PhoneBook(String f, String l, String p)

因为他们在同一个 class.

在数据结构方面,你这里的概念是错误的。首先是,这个:

ArrayList < Contact > PhoneBook = new ArrayList();

应该在 while 循环之外,这样对于整个应用程序,您不会在循环后替换 phone 书。打印它们,稍后只需使用

for(int i = 0; i < phoneBook.size(); i++)
    your printing

听起来您需要创建一些 Getters。大多数 IDE 会为您做这件事。

例如,在您的联系人 class 中添加:

public String getFirst(){ return first; }

然后对您想要的所有项目执行此操作。当你想打印它们时,在你的驱动程序中为每个循环设置一个循环 class 像这样:

for(Contact contact : PhoneBook){
    System.out.println("Contact details: " + contact.getFirst() + " " + contact.getLast() + ", Phone #: " + contact.getPhoneNumber());
}

或者,您也可以在联系人 class 中创建一个方法,从上面获取 println 内容并将其吐出。例如:

public void printContactDetails(){ System.out.println("...");}

然后在你的 for each 循环调用中:contact.printContactDetails();

您只需覆盖 Contact class 的 toString() 方法,然后在 main() 方法中,直接调用 ArrayListtoString().

这是我的例子:

package somepackage;

import java.util.ArrayList;

public class Test {

    public static void main(String[] args) {

        ArrayList<Inner> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Inner in = new Inner("name" + i, "address" + i);
            list.add(in);
        }
        System.out.println(list.toString());
    }


    private static class Inner {

        private String name;
        private String address;

        Inner(String name, String address) {
            this.name = name;
            this.address = address;
        }

        @Override
        public String toString() {
            return "name:" + name + ", " + "address: " + address + "\n";
        }
    }

}

屏幕输出:

[name:name0, address: address0
, name:name1, address: address1
, name:name2, address: address2
, name:name3, address: address3
, name:name4, address: address4
, name:name5, address: address5
, name:name6, address: address6
, name:name7, address: address7
, name:name8, address: address8
, name:name9, address: address9
]

好的,在大佬们的帮助下搞定了!我更改了 if 语句,因此您现在可以添加新联系人、显示 phone 图书或退出。我还添加了 phone 号码验证!如果有人关心,这是更新后的代码!

import java.util.Scanner;
import java.util.ArrayList;
public class contactDriver {
  public static void main(String[] args) {
      String first; //first name
      String last; //last name
      String phone = ""; //phone number
      String input; //answer to create a new contact

      boolean add = true; //boolean to add new contact
      boolean phoneValid; //boolean to validate phone number

      Scanner scan = new Scanner(System.in);

      ArrayList < Contact > PhoneBook = new ArrayList < > ();

      while (add) {
        phoneValid = false;

        System.out.println("Type (N) to add a new contact, (D) to display your phonebook, or (Q) to quit!");
        input = scan.nextLine();
        if (input.equalsIgnoreCase("N")) {
          System.out.println("Enter the contact's first name: ");
          first = scan.nextLine();

          System.out.println("Enter the contact's last name: ");
          last = scan.nextLine();

          while (!phoneValid) {
            System.out.println("Enter the contact's phone number: XXX-XXX-XXXX");
            phone = scan.nextLine();
            if (phone.matches("\d{3}[-\.\s]\d{3}[-\.\s]\d{4}")) {
              phoneValid = true;
              break;
            } else {
              System.out.println("Sorry, I didn't catch that!");
            }
          }

          Contact contact = new Contact(first, last, phone);
          PhoneBook.add(contact);
        } else if (input.equalsIgnoreCase("Q")) {
          add = false;
          System.out.println("Goodbye!");
          break;
        } else if (input.equalsIgnoreCase("D")) {
          for (Contact c: PhoneBook) {
            System.out.println(c);
          }
        } else {
          System.out.println("Sorry, I didn't catch that!");
        }
      }
    } //end main
} //end Class ComputerDriver