序列化Arraylists [英] Serializing Arraylists

查看:85
本文介绍了序列化Arraylists的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我想知道你们是否可以帮我将ArrayList序列化到一个文件上。现在我正在从学校开始总结,但是我没有真正的编码问题。我正在制作的是一个GUI地址簿,当用户添加地址时,它会存储在一个arraylist中并添加到comboBox中。这就是我在做的事情。我要问的是你是否可以序列化Arraylists。如果可以,你们可以教我怎么样?或者至少给我一些教程?

Now I am wondering if you guys could help me on serializing an ArrayList onto a file. Now I am working on a summative from school, however there is no real coding problem I am having. What I am making is a GUI address book, and when a user adds an address it is stored into an arraylist and added to the comboBox. That is what I am doing. What I am asking is if you can serialize Arraylists. If you can, can you guys teach me how? Or at the very least give me some tutorials?

非常感谢你。如果你们在回答之前需要查看代码,请告诉我,我会告诉你。再次,非常感谢你。

Thank you so much. If you guys need to see code before answering, just let me know and I will show it. Again, thank you so much.

好的,这是我的整个代码:

Okay here is my entire code:

import java.awt.EventQueue;


public class Address_Book {

    private JFrame frame;
    private JTextField newName;
    private JTextField newAddress;
    private JTextField newPhoneAddress;
    ArrayList<Book> test = new ArrayList<Book>();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Address_Book window = new Address_Book();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Address_Book() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 371, 262);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        final JComboBox comboBox = new JComboBox();
        final DefaultComboBoxModel model = new DefaultComboBoxModel();

        newName = new JTextField();
        newName.setBounds(10, 29, 79, 20);
        frame.getContentPane().add(newName);
        newName.setColumns(10);

        JLabel lbl1 = new JLabel("Enter New Name:");
        lbl1.setBounds(10, 11, 107, 14);
        frame.getContentPane().add(lbl1);

        JLabel lbl2 = new JLabel("Enter New Address:");
        lbl2.setBounds(110, 11, 107, 14);
        frame.getContentPane().add(lbl2);

        newAddress = new JTextField();
        newAddress.setColumns(10);
        newAddress.setBounds(109, 29, 96, 20);
        frame.getContentPane().add(newAddress);

        newPhoneAddress = new JTextField();
        newPhoneAddress.setColumns(10);
        newPhoneAddress.setBounds(215, 29, 130, 20);
        frame.getContentPane().add(newPhoneAddress);

        JLabel lbl3 = new JLabel("Enter New Phone number:");
        lbl3.setBounds(215, 11, 140, 14);
        frame.getContentPane().add(lbl3);

        JButton btnAddNewContact = new JButton("Add new contact");
        btnAddNewContact.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent arg0) {
                test.add( (new Book(newName.getText(), newAddress.getText(), newPhoneAddress.getText())));
                //mergesort.mergesort(test, 0, test.size() - 1);
                model.removeAllElements();
                for(int i=0; i < test.size();i++){
                    model.addElement(test.get(i).getContact()); 
                }
                comboBox.setModel(model);
                newName.setText(""); 
                newAddress.setText("");
                newPhoneAddress.setText("");
            }
        });
        btnAddNewContact.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnAddNewContact.setBounds(10, 53, 335, 23);
        frame.getContentPane().add(btnAddNewContact);

        JLabel lbl4 = new JLabel("Current Contacts:");
        lbl4.setBounds(10, 87, 107, 14);
        frame.getContentPane().add(lbl4);

        comboBox.setModel(model);
        comboBox.setBounds(10, 101, 335, 20);
        comboBox.setSelectedIndex(test.size()-1);
        frame.getContentPane().add(comboBox);
    }
}

这是我的对象:

public class Book implements Comparable {
     private String flName, Address, pNumber;

    public Book(String Name, String address, String phoneNumber ){
        setFlName(Name);
        setAddress(address);
        setpNumber(phoneNumber);
    }

    public String getpNumber() {
        return pNumber;
    }

    public void setpNumber(String pNumber) {
        this.pNumber = pNumber;
    }

    public String getAddress() {
        return Address;
    }

    public void setAddress(String address) {
        Address = address;
    }

    public String getFlName() {
        return flName;
    }

    public void setFlName(String flName) {
        this.flName = flName;
    }  

    public String getContact() {
        return flName + " " + Address + " " + pNumber;
    }

    public int compareTo(Object c) {
        Book testBook = (Book)c;

        if (testBook.getFlName().compareTo(this.getFlName()) < 0){
            return(-1);
        }else if(testBook.getFlName().compareTo(this.getFlName()) == 0){
            return(0);
        }else{
            return(1);
        }
    }

}

现在,再次,我不确定如何使Arraylists可序列化。我使用过对象,但没有使用Arraylists,所以我不知道它是否是同一个进程。我只是在寻求建议,或者是一些关于使ArrayLists可序列化的好教程。

Now, again, I am unsure of how to proceed to make Arraylists serializable. I have used objects, but not Arraylists, so I do not know whether or not it is the same process. I am just merely asking for advice, or some great tutorials on making ArrayLists serializable.

推荐答案

来自 ArrayList 文档此处


所有已实现的接口:
可序列化,可克隆,Iterable,Collection,List,RandomAccess

All Implemented Interfaces: Serializable, Cloneable, Iterable, Collection, List, RandomAccess

所以是的,它是可序列化的。在Java中使用任何其他流非常简单,只需看看 ObjectOutputStream ObjectInputStream

So yes, it's serializable. Doing it is quite easy as using any other stream in Java, just take a look at ObjectOutputStream and at ObjectInputStream.

这篇关于序列化Arraylists的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆