试图让我的arraylist打印给定的项目

时间:2014-11-17 03:35:47

标签: java arraylist

我正在尝试使用Java创建联系人列表。我想我已经掌握了大部分内容,但我确信它可以增强。我相信我可以在arraylist中添加项目,但是,当我尝试打印arraylist时,我会得到一些随机文本和数字。此外,我不是因为通过联系人ID号识别然后只打印该联系人而迷失​​方向。对材料的任何帮助或参考都会有很大帮助!我试过通过教科书和在线研究,说实话,现在我只是更加困惑。这是我的代码: 主:

package contactslist;
import java.util.*;
import java.io.*;
public class ContactsList {
public static void main(String[] args) {
    Scanner input1 = new Scanner(System.in);
    int type = 0;
    ArrayList<Contacts> contacts = new ArrayList<Contacts>();
    while(type != 4){
    System.out.println("[1] Personal Contact");
    System.out.println("[2] Business Contact");
    System.out.println("[3] Display Contacts");
    System.out.println("[4] to quit");
    type = input1.nextInt();
    if(type == 4){
        System.out.println("Goodbye");
        break;
        }
    else if (type == 3){
        int totalContacts = contacts.size();
        for (int i = 0; i < totalContacts; i++){
            System.out.print(contacts);
        }
    }
    Scanner inputs = new Scanner(System.in);
    System.out.println("Please enter a numeric ContactId: ");
    String contactId = inputs.nextLine();
    System.out.println("Please enter First Name: ");
    String firstName = inputs.nextLine();
    if (firstName == null) {
        break;
    }
    System.out.println("Please enter Last Name: ");
    String lastName = inputs.nextLine();
    if (lastName == null) {
        break;
    }
    System.out.println("Please enter Address: ");
    String address = inputs.nextLine();
    System.out.println("Please enter Phone Number: ");
    String phoneNumber = inputs.nextLine();
    System.out.println("Please enter Email Address: ");
    String emailAddress = inputs.nextLine();
    if(type == 1){
       System.out.println("Please enter Birthday: ");
       String dateofBirth = inputs.nextLine();
       Contacts personal = new PersonalContact(contactId, firstName, lastName, address,
phoneNumber, emailAddress, dateofBirth);
       contacts.add(personal);
    }
    else if(type == 2){
        System.out.println("Please enter Job Title: ");
        String jobTitle = inputs.nextLine();
        System.out.println("Please enter Organization: ");
        String organization = inputs.nextLine();
        Contacts business = new BusinessContact(contactId, firstName, lastName,
address, phoneNumber, emailAddress, jobTitle, organization);
        contacts.add(business);
    }
    }
    }
    }  

联系人类:

package contactslist;
import java.util.*;
import java.io.*;
public abstract class Contacts {
String contactId;
String firstName;
String lastName;
String address;
String phoneNumber;
String emailAddress;
public Contacts(String contactId,String firstName,String lastName, String address,
String phoneNumber, String emailAddress)
{
    this.contactId = contactId;
    this.firstName = firstName;
    this.lastName = lastName;
    this.address = address;
    this.phoneNumber = phoneNumber;
    this.emailAddress = emailAddress;
}
public void setContactId(String input){
    this.contactId = input;
}
public String getContactId(){
    return contactId;
}
public void setFirstName(String input){
    this.firstName = input;
}
public String getFirstName(){
    return firstName;
}
public void setLastName(String input){
    this.lastName = input;
}
public String getLastName(){
    return lastName;
}
public void setAddress(String input){
    this.address = input;
}
public String getAddress(){
    return address;
}
public void setPhoneNumber(String input){
    this.phoneNumber = input;
}
public String getPhoneNumber(){
    return phoneNumber;
}
public void setEmailAddress(String input){
    this.emailAddress = input;
}
public String getEmailAddress(){
    return emailAddress;        
}
public void displayContacts(){
    System.out.println("Contact ID: " + contactId + " First Name: " + firstName + "
Last Name: " + lastName);
}
}

个人子类:

package contactslist;
import java.util.*;
import java.io.*;
public class PersonalContact extends Contacts{
private String dateofBirth;
public PersonalContact(String contactId, String firstName, String lastName, String
address, String phoneNumber, String emailAddress, String dateofBirth){
super(contactId, firstName, lastName, address, phoneNumber, emailAddress);
this.dateofBirth = dateofBirth;
}
public void setDateofBirth(String input){
this.dateofBirth=input;
}
public String getDateofBirth(){
return this.dateofBirth;
}
@Override
public void displayContacts(){
System.out.print("Personal Contacts: ");
System.out.println("Contact ID: " + contactId + " First Name: " + firstName + " Last
Name: " + lastName);
System.out.println("Address: " + address);
System.out.println("Phone Number: " + phoneNumber);
System.out.println("Email Address: " + emailAddress);
System.out.println("Birthday: " + dateofBirth); 
}
}

业务子类:

package contactslist;
public class BusinessContact extends Contacts{
private String jobTitle;
private String organization;
public BusinessContact(String contactId, String firstName, String lastName, String
address, String phoneNumber, String emailAddress, String jobTitle, String organization)
{
super(contactId, firstName, lastName, address, phoneNumber, emailAddress);
this.jobTitle = jobTitle;
this.organization = organization;
}
public void jobTitle(String input){
this.jobTitle = jobTitle;
}
public String getjobTitle(){
return this.jobTitle;
}
public void organization(String input) {
this.organization = organization;
}
public String getOrganization(){
return this.organization;
}
@Override
public void displayContacts(){
System.out.print("Personal Contacts: ");
System.out.println("First Name: " + firstName + " Last Name :" + lastName);
System.out.println("Address: " + address);
System.out.println("Phone Number: " + phoneNumber);
System.out.println("Email Address: " + emailAddress);
System.out.println("Job Title: " + jobTitle);
System.out.println("Orgnanization: " + organization);
}   
}

这是我选择选项3时显示联系人的内容。

错误:

[contactslist.PersonalContact@1df38fd]

非常感谢任何帮助。我疯了,请原谅我这个问题。我已经尝试了一些我用Google搜索过的不同的东西,我只是没有得到它。有人能指出我正确的方向,还是给我一个很好的网站参考?

1 个答案:

答案 0 :(得分:1)

您需要编写自己的方法来打印该ArrayList。类似的东西:

public void printAllContacts(ArrayList<Contacts> contacts) {
    for (Contacts c : contacts) {
      c.displayContacts();
    }
}

并为选项3调用而不是System.out.println(contacts);(Java只会打印有关该对象的信息)。

在此处阅读有关ArrayList的更多信息:https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html