gpt4 book ai didi

java - 尝试让我的数组列表打印给定的项目

转载 作者:行者123 更新时间:2023-12-01 12:16:30 25 4
gpt4 key购买 nike

我正在尝试获取用 Java 创建的联系人列表。我想我已经拥有了大部分,尽管我确信它可以得到增强。我相信我可以将项目添加到数组列表中,但是,当我尝试打印数组列表时,我得到了一些随机文本和数字。另外,我并没有迷失通过联系人 ID 号码进行识别,然后只是打印该联系人。任何帮助或引用 Material 都会有很大帮助!我尝试过阅读教科书并在网上进行研究,说实话,现在我只是更困惑了。这是我的代码:主要:

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]

任何帮助将不胜感激。我快疯了,请原谅我的问题。我尝试了一些不同的东西,我在谷歌上搜索过,但我就是不明白。有人能给我指出正确的方向,或者给我一个好的网站可供引用吗?

最佳答案

您需要编写自己的方法来打印该 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

关于java - 尝试让我的数组列表打印给定的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26965109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com