| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1359 人关注过本帖
标题:使用对象化序列存储ArrayList有问题,谁能指导一下?谢谢
只看楼主 加入收藏
Vsnow
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:124
专家分:145
注 册:2015-1-3
结帖率:95%
收藏
 问题点数:0 回复次数:2 
使用对象化序列存储ArrayList有问题,谁能指导一下?谢谢
代码如下
hwk4.zip (2.86 KB)
2016-11-27 19:47
Vsnow
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:124
专家分:145
注 册:2015-1-3
收藏
得分:0 
package hwk4;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import

public class Owner implements Serializable {
   
    /**
    13      * 序列化ID
    14      */
    private static final long serialVersionUID = -5809782578272943999L;
   
    protected String ownerName;// 车主
    protected int tele;// 联系方式
    protected String ID;// 身份证号
    protected String license;// 行驶证号
    protected boolean policyOfInsurance;// 是否有保险单
    protected boolean invoice;// 是否有发票
    protected String sellExplain;// 卖车说明

    public Owner() {
    }// 构造空函数

    public Owner(String ownerName, int tele, String iD, String license,
            boolean policyOfInsurance, boolean invoice, String sellExplain) {
        super();
        this.ownerName = ownerName;
        this.tele = tele;
        ID = iD;
        this.license = license;
        this.policyOfInsurance = policyOfInsurance;
        this.invoice = invoice;
        this.sellExplain = sellExplain;
    }

    @Override
    public String toString() {
        return "Owner [ownerName=" + ownerName + ", tele=" + tele + ", ID="
                + ID + ", license=" + license + ", policyOfInsurance="
                + policyOfInsurance + ", invoice=" + invoice + ", sellExplain="
                + sellExplain + "]"+"\n";
    }

    Scanner sca = new Scanner(System.in);

    // 查询车主信息
    public ArrayList<Owner> SearchOwner(ArrayList<Owner> ownerList) {
        Iterator<Owner> it = ownerList.iterator();// 创建一个ownerList集合对应的迭代器
        ArrayList<Owner> downer = new ArrayList<Owner>();

        System.out.println("请输入车主姓名::");
        String str = sca.next();
        while (it.hasNext()) {
            Owner sowner = (Owner) it.next();
            if (sowner.ownerName.endsWith(str)) {
                downer.add(sowner);// 把符合条件的车辆放入一个dcar 集合中
            }
        }
        return downer;
    }

    // 删除车主信息
    public ArrayList<Owner> DeleteOwner(ArrayList<Owner> ownerList) {
        Iterator<Owner> it = ownerList.iterator();// 创建一个 ownerList集合对应的迭代器
        ArrayList<Owner> downer = new ArrayList<Owner>();

        System.out.println("请输入您需要删除的车主姓名:");
        String str = sca.next();
        while (it.hasNext()) {
            Owner mowner = (Owner) it.next();
            if (mowner.ownerName.endsWith(str)) {
                downer.add(mowner);// 把符合删除条件的车辆放入一个mowner 集合中
            }
        }
        return downer;
    }

    // 修改车主信息
    public Owner ModifyOwner(ArrayList<Owner> ownerList) {
        Owner owner = new Owner();
        System.out.println("您需要修改第几条信息:");
        int mn = sca.nextInt();
        for (int i = 0; i < ownerList.size(); i++) {
            if (i + 1 == mn) {
                ownerList.remove(i);
            }
        }
        System.out.println("请按以下顺序输入您需要修改的信息:");
        System.out
                .println("--车主姓名--联系方式--身份证号--行驶证号--是否有保险单true OR false--是否有发票true OR false--买车说明--");
        String s = sca.next();
        owner.ownerName = s;
        int i1 = sca.nextInt();
        owner.tele = i1;
        String s1 = sca.next();
        owner.ID = s1;
        String s2 = sca.next();
        owner.license = s2;
        boolean b1 = sca.hasNext();
        owner.policyOfInsurance = b1;
        boolean b2 = sca.hasNext();
        owner.invoice = b2;
        String s4 = sca.next();
        owner.sellExplain = s4;
        return owner;
    }

    // 增加车主信息
    public ArrayList<Owner> AddOwner(ArrayList<Owner> ownerList) {
        Owner owner = new Owner();
        System.out.println("请输入您需要增加的个数:");
        int dn = sca.nextInt();
        System.out.println("请按以下顺序输入您需要添加的信息:");
        System.out
                .println("--车主姓名--联系方式--身份证号--行驶证号--是否有保险单true OR false--是否有发票true OR false--买车说明--");
        for (int i = 0; i < dn; i++) {
            String s = sca.next();
            owner.ownerName = s;
            int i1 = sca.nextInt();
            owner.tele = i1;
            String s1 = sca.next();
            owner.ID = s1;
            String s2 = sca.next();
            owner.license = s2;
            boolean b1 = sca.hasNext();
            owner.policyOfInsurance = b1;
            boolean b2 = sca.hasNext();
            owner.invoice = b2;
            String s4 = sca.next();
            owner.sellExplain = s4;
            ownerList.add(owner);
        }
        return ownerList;
    }
//添加get和set方法


    public String getOwnerName() {
        return ownerName;
    }

    public int getTele() {
        return tele;
    }

    public String getID() {
        return ID;
    }

    public String getLicense() {
        return license;
    }

    public boolean isPolicyOfInsurance() {
        return policyOfInsurance;
    }

    public boolean isInvoice() {
        return invoice;
    }

    public String getSellExplain() {
        return sellExplain;
    }
   
    public void setOwnerName(String ownerName) {
        this.ownerName = ownerName;
    }

    public void setTele(int tele) {
        this.tele = tele;
    }

    public void setID(String iD) {
        ID = iD;
    }

    public void setLicense(String license) {
        this.license = license;
    }

    public void setPolicyOfInsurance(boolean policyOfInsurance) {
        this.policyOfInsurance = policyOfInsurance;
    }

    public void setInvoice(boolean invoice) {
        this.invoice = invoice;
    }

    public void setSellExplain(String sellExplain) {
        this.sellExplain = sellExplain;
    }
   
}
2016-11-27 19:48
Vsnow
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:124
专家分:145
注 册:2015-1-3
收藏
得分:0 
package hwk4;

import *;
import java.util.ArrayList;
import java.util.Iterator;

public class IOstream {// 使用对象序列化存储
   
    public static void main(String[] args) {
        IOstream io = new IOstream();
        io.saveOwnerObj();
        io.readOwnerObj();
    }

    public void saveOwnerObj() {
        ArrayList<Owner> ownerList = new ArrayList<Owner>();
        Owner ner1 = new Owner("张一", 188177, "1234e", "234rr", true, false,
                "性价比高!");
        Owner ner2 = new Owner("张二", 188222, "3434e", "2eerr", true, false,
                "外观漂亮!");
        ownerList.add(ner1);
        ownerList.add(ner2);
        try {
            FileOutputStream f = new FileOutputStream("owner.ser");
            ObjectOutputStream os = new ObjectOutputStream(f);
            os.writeObject(ownerList);
            os.close();
            f.close();
            System.out.println("(存储)文件创建成功!");
        } catch (IOException e) {
            System.out.println("(存储)文件创建错误!");
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    public void readOwnerObj() {
        try {
            ArrayList<Owner> tempList = new ArrayList<Owner>();
            FileInputStream f = new FileInputStream("owner.ser");
            ObjectInputStream is = new ObjectInputStream(f);
            ArrayList<Owner> ner = new ArrayList<Owner>();
            tempList = (ArrayList<Owner>) is.readObject();
            Iterator<Owner> it = tempList.iterator();
            while (it.hasNext()) {
                Owner mner = (Owner) it.next();
                ner.add(mner);
            }
            is.close();
            System.out.println("read object success!");
        } catch (IOException e) {
            System.out.println("(读取)文件创建错误!");
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            System.out.println("(读取)没有找到该类!");
            e.printStackTrace();
        }
    }
}
2016-11-27 19:48
快速回复:使用对象化序列存储ArrayList有问题,谁能指导一下?谢谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015478 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved