java.io.NotSerializableException-联系人列表程序
嘿,我一直在努力让我的写和读方法发挥作用,但我不知道怎么了 除了这些方法不能正常工作之外,我还必须在TestDiskSaveing类中证明序列化工作正常。这就是为什么我把它设为null。 有什么想法吗java.io.NotSerializableException-联系人列表程序,java,Java,嘿,我一直在努力让我的写和读方法发挥作用,但我不知道怎么了 除了这些方法不能正常工作之外,我还必须在TestDiskSaveing类中证明序列化工作正常。这就是为什么我把它设为null。 有什么想法吗 public class TestDiskSaving { public static void main(String args[]) { ContactList myList = new ContactList(); myList.addContact
public class TestDiskSaving {
public static void main(String args[]) {
ContactList myList = new ContactList();
myList.addContact();
myList.addContact();
myList.printAll();
myList.find();
myList.write();
myList = null;
System.out.println(myList);
myList.read();
System.out.println(myList);
}
}
公共类联系人列表{
private ArrayList currentContacts=新建ArrayList();
/**
*在下一个未占用索引处将联系人添加到列表中
*
*@作者尼克
*/
public void addContact(){
currentContacts.add(newContact());
}
/**
*返回联系人列表的大小
*
*@作者尼克
*/
公共int getSize(){
返回currentContacts.size();
}
/**
*接受用户输入并返回新联系人
*/
私人联系人newContact(){
扫描仪扫描=新扫描仪(System.in);
System.out.println(“请输入此人的名字”);
字符串newFirstName=scan.nextLine();
System.out.println(“请输入此人的姓氏”);
字符串newLastName=scan.nextLine();
如果(!newLastName.trim().equals(null)&&!newLastName.trim().equals(“”){
联系人联系人=新联系人(新姓氏);
contact.setFirstName(newFirstName);
System.out.println(“请输入此人的地址”);
contact.setStreetAddress(scan.nextLine());
System.out.println(“请输入此人的电子邮件地址”);
contact.setEmailAddress(scan.nextLine());
System.out.println(“请输入此人的电话号码”);
contact.setPhoneNumber(scan.nextLine());
System.out.println(“请输入有关此人的任何其他附加信息”);
contact.setNotes(scan.nextLine());
System.out.println(newFirstName+“”+newLastName+“已存储到联系人列表中”);
返回联系人;
}否则{
System.out.println(“您没有输入此人的姓氏。”+“\n”
+“此人未被储存在联系人名单中。”);
返回null;
}
}
/**
*按姓氏字母顺序打印列表中存储的所有联系人,然后
*名字。(不区分大小写)
*
*@作者尼克
*/
public void printAll(){
ArrayList打印列表=当前联系人;
Collections.sort(打印列表);
对于(int i=0;i
公共类联系人实现可比较{
私有字符串名;
私有字符串lastName;
私有字符串streetAddress;
私有字符串电子邮件地址;
私有字符串电话号码;
私人弦乐;
/**
*设置联系人的姓氏
*
*@作者阿曼
*/
公共联系人(字符串lastName){
this.lastName=lastName;
}
/**
*返回联系人的姓氏
*@作者志祥
*/
公共字符串getLastName(){
返回姓氏;
}
/**
*设置联系人的姓氏
*@作者尼克
*/
public void setLastName(字符串newLastName){
this.lastName=newLastName;
}
/**
*打印出联系人上的所有数据,格式化后供用户查看。
*@作者志祥
*/
public void printInfo(){
System.out.println(“名称:“+getFirstName()+”+getLastName());
System.out.println(“Phone:+getPhoneNumber());
System.out.println(“地址:+getStreetAddress());
System.out.println(“Email:+getEmailAddress());
System.out.println(“Notes:+getNotes());
System.out.println();
}
/**
*返回联系人的名字
*
* @
public class ContactList {
private ArrayList<Contact> currentContacts = new ArrayList<Contact>();
/**
* Adds a contact to the list at the next unoccupied index
*
* @author Nick
*/
public void addContact() {
currentContacts.add(newContact());
}
/**
* returns the size of the contact list
*
* @author Nick
*/
public int getSize() {
return currentContacts.size();
}
/**
* take user input and return a new contact
*/
private Contact newContact() {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the person's first name.");
String newFirstName = scan.nextLine();
System.out.println("Please enter the person's last name.");
String newLastName = scan.nextLine();
if (!newLastName.trim().equals(null) && !newLastName.trim().equals("")) {
Contact theContact = new Contact(newLastName);
theContact.setFirstName(newFirstName);
System.out.println("Please enter the person's address.");
theContact.setStreetAddress(scan.nextLine());
System.out.println("Please enter the person's email address.");
theContact.setEmailAddress(scan.nextLine());
System.out.println("Please enter the person's phone number.");
theContact.setPhoneNumber(scan.nextLine());
System.out.println("Please enter any other additional information about the person.");
theContact.setNotes(scan.nextLine());
System.out.println(newFirstName + " " + newLastName + " got stored into the contact list");
return theContact;
} else {
System.out.println("You did not enter the person's last name." + "\n"
+ "The person did not get stored into the contact list.");
return null;
}
}
/**
* Prints all contacts stored in the list, alphabetically by last name, then
* first name. (not case sensitive)
*
* @author Nick
*/
public void printAll() {
ArrayList<Contact> printList = currentContacts;
Collections.sort(printList);
for (int i = 0; i < printList.size(); i++) {
printList.get(i).printInfo();
}
}
/**
* Find contact(s) in contact list by last name
*
* @author Zhixiang
*/
public void find() {
Scanner scan = new Scanner(System.in);
String lastName;
System.out.println("Please enter a last name for search:");
lastName = scan.nextLine();
boolean found = false;
for (int i = 0; i < currentContacts.size(); i++) {
if (currentContacts.get(i).getLastName().trim().equalsIgnoreCase(lastName)) {
currentContacts.get(i).printInfo();
found = true;
}
}
if (!found) {
System.out.println("Sorry. Contact with last name: " + lastName + " not found.");
}
}
/**
* Saves the current list of contacts onto the disk
*
* @author Arman
*/
public void write() {
FileOutputStream outFile;
ObjectOutputStream outObject;
try {
outFile = new FileOutputStream("data");
outObject = new ObjectOutputStream(outFile);
outObject.writeObject(currentContacts);
outObject.writeObject(currentContacts);
outFile.close();
outObject.close();
} catch (IOException ioe) {
System.out.println("Error writing objects to the file: " + ioe.getMessage());
}
}
/**
* Reads any saved data from the disk into memory, and stores it in the
* currentContacts
*
* @author Arman
*/
public void read() {
FileInputStream inFile;
ObjectInputStream inObject;
try {
inFile = new FileInputStream("data");
inObject = new ObjectInputStream(inFile);
currentContacts = (ArrayList<Contact>) inObject.readObject();
currentContacts = (ArrayList<Contact>) inObject.readObject();
inFile.close();
inObject.close();
} catch (IOException ioe) {
System.out.println("Error reading from the file: " + ioe.getMessage());
} catch (ClassNotFoundException cnfe) {
System.out.println(cnfe);
}
}
}
public class Contact implements Comparable <Contact> {
private String firstName;
private String lastName;
private String streetAddress;
private String emailAddress;
private String phoneNumber;
private String notes;
/**
* Set the last name for contact
*
* @author Arman
*/
public Contact(String lastName) {
this.lastName = lastName;
}
/**
* Return last name of the contact
* @author Zhixiang
*/
public String getLastName(){
return lastName;
}
/**
* Sets the last name of the contact
* @author Nick
*/
public void setLastName(String newLastName){
this.lastName = newLastName;
}
/**
* Prints out all data on the contact, formatted for user viewing.
* @author Zhixiang
*/
public void printInfo(){
System.out.println("Name: "+ getFirstName() + " " + getLastName());
System.out.println("Phone: "+ getPhoneNumber());
System.out.println("Address: "+ getStreetAddress());
System.out.println("Email: "+ getEmailAddress());
System.out.println("Notes: "+ getNotes());
System.out.println();
}
/**
* Returns the contact's first name
*
* @author Nick
*/
public String getFirstName() {
return firstName;
}
/**
* Assigns the contact's first name
*
* @author Nick
*/
public void setFirstName(String newFirstName) {
firstName = newFirstName;
}
/**
* Returns the contact's street address
*
* @author Nick
*/
public String getStreetAddress() {
return streetAddress;
}
/**
* Assigns the contact's street address
*
* @author Nick
*/
public void setStreetAddress(String newStreetAddress) {
streetAddress = newStreetAddress;
}
/**
* Returns the contact's email address
*
* @author Nick
*/
public String getEmailAddress() {
return emailAddress;
}
/**
* Assigns the contact's email address
*
* @author Nick
*/
public void setEmailAddress(String newEmailAddress) {
this.emailAddress = newEmailAddress;
}
/**
* Returns the contact's phone number
*
* @author Nick
*/
public String getPhoneNumber() {
return phoneNumber;
}
/**
* Assigns the contact's phone number
*
* @author Nick
*/
public void setPhoneNumber(String newPhoneNumber) {
this.phoneNumber = newPhoneNumber;
}
/**
* Returns the contact's notes
*
* @author Nick
*/
public String getNotes() {
return notes;
}
/**
* Assigns the contact's notes
*
* @author Nick
*/
public void setNotes(String newNotes) {
this.notes = newNotes;
}
/**
* Return formatted entire contact information
* @author Arman
*/
public String toString(){
return firstName + " " + lastName + " " + streetAddress + " " + emailAddress + " " + phoneNumber + " " + notes ;
}
/**
* Allow making comparison between contacts for sorting
* Compares this contact to the contact you specify. Compares by last name then first name alphabetically.
* Returns -1 if this contact comes before the other, returns 0 if they are exactly the same name, returns 1 if this comes after the other one.
* @author Nick
*/
public int compareTo(Contact otherContact){
if (this.lastName.toLowerCase().compareTo(otherContact.lastName.toLowerCase()) <= -1) {
return -1;
}
else if (this.lastName.toLowerCase().compareTo(otherContact.lastName.toLowerCase()) >= 1){
return 1;
}
else if (this.firstName.toLowerCase().compareTo(otherContact.firstName.toLowerCase()) <= -1){
return -1;
}
else if (this.firstName.toLowerCase().compareTo(otherContact.firstName.toLowerCase()) >= 1){
return 1;
}
else return 0;
}
}
public class Contact implements Comparable<Contact>, Serializable {
private static final long serialVersionUID = 1L;
}
public class ContactList {
private static final long serialVersionUID = 1L;
}