序列化链表

时间:2012-07-06 16:21:35

标签: serialization linked-list

我一直在尝试创建一个地址簿,但似乎无法序列化我存储了联系人的链表。我没有收到错误或异常,但我似乎也无法序列化列表。这是我的序列化和反序列化发生的对象的代码:

import java.io.*;

public class contacts implements Serializable
{
    private contactNode list;
    FileOutputStream file;
    ObjectOutputStream outStream;
    FileInputStream file2;
    ObjectInputStream inStream;


    public contacts() throws IOException
    {
        file2 = new FileInputStream("contacts.dat");
        inStream = new ObjectInputStream(file2);

        try
        {
            list = (contactNode)inStream.readObject();
        }
        catch(ClassNotFoundException e)
        {
            list = null;
        }
        catch(EOFException e)
        {

        }
        catch(WriteAbortedException e)
        {
            list = null;
        }
        catch(NotSerializableException e)
        {
            list = null;
        }
    }

    public void addContact(contact cnt)
    {
        contactNode node = new contactNode(cnt);
        contactNode current;

        String newName = cnt.toStringNameFML();
        String name;

        if(list == null)
        {
            list = node;
        }

        else
        {
            current = list;
            while(current.next != null)
            {
                current = current.next;
            }
            current.next = node;
        }
    }

    public void deleteContact(contact cnt)
    {
        contactNode current = list.next;
        contactNode last = list;
        String newName;
        if(list.next == null)
        {

        }
        else
        {
            if(cnt.toString().equals(list.cntct.toString()))
            {
                list = current;
            }
            else
            {
                while(current.next != null)
                {
                    newName = current.cntct.toString();
                    if(cnt.toString().equals(newName))
                    {
                        last.next = current.next;
                    }
                    last = current;
                    current = current.next;
                }
            }
        }
    }

    public contact findContact(String name)
    {
        contactNode current = list;

        while(current.next != null)
        {
            if(current.cntct.toStringNameFML().equals(name))
            {
                return current.cntct;
            }
            current = current.next;
        }
        return current.cntct;
    }

    public String[] getNames()
    {
        contactNode current = list;
        String[] names;
        int numNames = 0;

        while(current != null)
        {
            numNames++;
            current = current.next;
        }

        names = new String[numNames];
        current = list;

        for(int k = 0; current != null; k++)
        {
            names[k] = current.cntct.toStringNameFML();
            current = current.next;
        }

        return names;
    }

    public void saveContacts() throws IOException
    {
        file = new FileOutputStream("contacts.dat");
        outStream = new ObjectOutputStream(file);
        outStream.writeObject(list);
        outStream.close();
        file.close();
    }

    private class contactNode implements Serializable
    {
        public contact cntct;
        public contactNode next;

        public contactNode(contact c)
        {
            cntct = c;
            next = null;
        }
    }
}

0 个答案:

没有答案