如何在 ArrayList 类中添加新的用户数据
抱歉,如果我没有正确解释这一点,但是我将如何添加新的联系信息。
我正在开发一个个人项目,并尝试将数据添加到数组列表中。我创建了供用户输入信息的输入,但如何将信息添加到数组列表中?
ArrayList contactLists = new ArrayList<>();
主类:
package com.ContactList;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
ArrayList<ContactList> contactLists = new ArrayList<>();
public static void main(String[] args) {
displayMen();
}
public static void displayMen() {
do {
System.out.println("Please choose from the following selection \n press 2 add a business contact \n press 2 to a personal contact \n press 3 to display your contact list");
Scanner sc = new Scanner(System.in);
int select = sc.nextInt();
switch (select) {
case 1:
//use this to add a business contact
// System.out.println("This is a test ot see the code is working");
addContact();
break;
case 2:
//use this to add a personal contact
break;
case 3:
//allow the user to display the contact information
break;
case 4:
//this is to quit the program
}
} while (true);
}
public static void addContact() {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the user first name");
String fNmae = sc.next();
System.out.println("Please enter the user last name");
String lName = sc.next();
System.out.println("Please enter the user address");
String address = sc.next();
System.out.println("Please enter the user phoneNumber");
String phoneNumber = sc.next();
System.out.println("Please enter the user email");
String email = sc.next();
String data = (lName + lName + address + phoneNumber + email);
}
}
ContactList 类:
package com.ContactList;
public class ContactList {
String firstName;
String lastName;
String address;
String phoneNumber;
String email;
public void contactList(String firstName, String lastName, String address, String phoneNumber, String email){
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phoneNumber = phoneNumber;
this.email = email;
}
//Getter Methods
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getAddress() {
return address;
}
public int getPhoneNumber() {
return phoneNumber;
}
public String getEmail() {
return email;
}
}
Business Contact 类:
package com.ContactList;
public class BusinessContact extends ContactList{
String jobTitle;
String organization;
public void businessContact(String firstName, String lastName, String address, String phoneNumber, String email) {
super.contactList(firstName, lastName, address, phoneNumber, email);
this.jobTitle = jobTitle;
this.organization = organization;
}
}
PersonalContact 类:
public class PersonalContact extends ContactList{
int dateOfBirth;
public void PersonalContact(String firstName, String lastName, String address, int phoneNumber, String email, int dateOfBirth) {
super.contactList(firstName, lastName, address, phoneNumber, email);
this.dateOfBirth = dateOfBirth;
}
public int getDateOfBirth() {
return dateOfBirth;
}
}
Sorry if I'm not explaining this correctly, but how would I add new contact information into.
I'm working on a personal project and trying to add data into an arraylist list. I created input for the user to enter the information but how do I go about adding the information into the array list?
ArrayList contactLists = new ArrayList<>();
Main Class:
package com.ContactList;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
ArrayList<ContactList> contactLists = new ArrayList<>();
public static void main(String[] args) {
displayMen();
}
public static void displayMen() {
do {
System.out.println("Please choose from the following selection \n press 2 add a business contact \n press 2 to a personal contact \n press 3 to display your contact list");
Scanner sc = new Scanner(System.in);
int select = sc.nextInt();
switch (select) {
case 1:
//use this to add a business contact
// System.out.println("This is a test ot see the code is working");
addContact();
break;
case 2:
//use this to add a personal contact
break;
case 3:
//allow the user to display the contact information
break;
case 4:
//this is to quit the program
}
} while (true);
}
public static void addContact() {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the user first name");
String fNmae = sc.next();
System.out.println("Please enter the user last name");
String lName = sc.next();
System.out.println("Please enter the user address");
String address = sc.next();
System.out.println("Please enter the user phoneNumber");
String phoneNumber = sc.next();
System.out.println("Please enter the user email");
String email = sc.next();
String data = (lName + lName + address + phoneNumber + email);
}
}
ContactList Class:
package com.ContactList;
public class ContactList {
String firstName;
String lastName;
String address;
String phoneNumber;
String email;
public void contactList(String firstName, String lastName, String address, String phoneNumber, String email){
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phoneNumber = phoneNumber;
this.email = email;
}
//Getter Methods
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getAddress() {
return address;
}
public int getPhoneNumber() {
return phoneNumber;
}
public String getEmail() {
return email;
}
}
Business Contact class:
package com.ContactList;
public class BusinessContact extends ContactList{
String jobTitle;
String organization;
public void businessContact(String firstName, String lastName, String address, String phoneNumber, String email) {
super.contactList(firstName, lastName, address, phoneNumber, email);
this.jobTitle = jobTitle;
this.organization = organization;
}
}
PersonalContact class:
public class PersonalContact extends ContactList{
int dateOfBirth;
public void PersonalContact(String firstName, String lastName, String address, int phoneNumber, String email, int dateOfBirth) {
super.contactList(firstName, lastName, address, phoneNumber, email);
this.dateOfBirth = dateOfBirth;
}
public int getDateOfBirth() {
return dateOfBirth;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您可以在
addContact()
中创建一个新的PersonalContact
/BusinessContact
对象,并使用刚刚从用户那里获得的数据进行初始化,并使用 add() 方法将其添加到 ArrayList 的末尾。对于
BusinessContact
,构造函数参数列表中缺少职务和组织字段,因此您需要添加这些字段。对于
PersonalContact
,您已选择将 phoneNumber 和 dateOfBirth 字段存储为整数,因此您需要使用适当的Scanner
方法来获取该数据并进行适当的转换。也许您还应该向
addContact
方法指示联系人是BusinessContact
还是PersonalContact
,以便您知道要构造什么类型的对象结尾:contactLists.add(new BusinessContact(fName, lName, 地址, 电话号码, 电子邮件, jobTitle, 组织);
https ://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ArrayList.html#add(E)
Looks like you can just create a new
PersonalContact
/BusinessContact
object inside youraddContact()
, initialized with the data you just got from the user, and add that to the end of the ArrayList using theadd()
method.For
BusinessContact
, the job title and organization fields are missing from the constructor parameter list, so you will need to add those.For
PersonalContact
, you've chosen to store the phoneNumber and dateOfBirth fields as integers, so you'll need to use the appropriateScanner
methods to get that data and cast it appropriately.Perhaps you should also indicate to the
addContact
method whether the contact will be aBusinessContact
or aPersonalContact
so you know what type of object to construct at the end:contactLists.add(new BusinessContact(fName, lName, address, phoneNumber, email, jobTitle, organization);
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ArrayList.html#add(E)
从 OOP 的角度来看,您的代码存在多个问题。但是尝试直接回答你的问题,你必须做两件事:
您应该通过其构造函数创建一个新的 ClientList 对象,并使用您刚刚从用户读取的值填充其属性:
There are multiple problems with your code from OOP perspective. But tring dirrectlly to answer your question you have to do 2 things:
You should create a new ClientList object via it's constructor and populate it's attributes with the values that you have just read from the user: