在java

时间:2016-04-14 03:16:22

标签: java arrays arraylist printing display

我还是java的新手。我试图制作一个基本上将联系人添加到数组列表的程序。就创建新对象和设置名称/编号而言,我已经想到了一切。据我所知,它将它添加到阵列中,但是我不确定如何显示阵列?我想添加一段代码,在添加每个联系人后显示数组列表。

这是我的联系人类,不确定我是否需要电话簿方法......



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




这是我的司机班......



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
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:0)

如果仅用于打印,请覆盖toString类的Contact方法,如下所示:

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

然后,在您的main方法中,通过执行以下操作打印所有联系人:

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

此外,您应该创建一个电话簿,这是您的循环之外的ArrayList

您的Contact课程应定义为:

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();
}

答案 1 :(得分:0)

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

TableLayoutPanel

当你知道你也有

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)

应该在while循环之外,因此对于整个应用程序,循环后不会替换电话簿。要打印它们,稍后再使用

ArrayList < Contact > PhoneBook = new ArrayList();

答案 2 :(得分:0)

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

例如,在您的联系人类中添加以下内容:

public String getFirst(){ return first; }

然后为您想要的所有项目执行此操作。如果要打印出来,请在驱动程序类中为每个循环设置一个,如下所示:

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

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

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

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

答案 3 :(得分:0)

您只需覆盖toString()课程的Contact方法,并在main()方法中直接致电ArrayList的{​​{1}}。

以下是我的例子:

toString()

屏幕输出:

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";
        }
    }

}

答案 4 :(得分:0)

好的,感谢你的帮助!我更改了if语句,因此您现在可以添加新联系人,显示电话簿或退出。我还添加了电话号码验证!如果有人关心,这是更新后的代码!

{{1}}