抱歉,如果我沒有正確解釋這一點,但我將如何添加新的聯系資訊。
我正在做一個個人專案并嘗試將資料添加到 arraylist 串列中。我為用戶創建了輸入以輸入資訊,但是如何將資訊添加到陣列串列中?
ArrayList 聯系人串列 = 新的 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);
}
}
聯系人串列類:
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;
}
}
商務聯系類:
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;
}
}
uj5u.com熱心網友回復:
看起來您可以在 ArrayList 中創建一個新的PersonalContact/BusinessContact 物件addContact(),使用剛從用戶那里獲得的資料進行初始化,然后使用該add()方法將其添加到 ArrayList 的末尾。
對于BusinessContact,建構式引數串列中缺少職位和組織欄位,因此您需要添加它們。
對于PersonalContact,您已選擇將 phoneNumber 和 dateOfBirth 欄位存盤為整數,因此您需要使用適當的Scanner方法來獲取該資料并進行適當的轉換。
也許您還應該向該addContact方法指示聯系人是 aBusinessContact還是 aPersonalContact以便您知道最后要構造什么型別的物件:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/450940.html
