数组列表未找到对象

时间:2015-03-24 21:39:00

标签: java

所以我试图创建一个功能,可以保存两个当前对象,以便下次加载它们而不是再次创建它们。我的课程有问题:

public class Investor implements Serializable {

        private String investorName;
        private double investorCapital;
        private ArrayList<Share> shareOwned;
        private ArrayList<Integer> numberOwned;

        public Investor(String name, double capital) {
            investorName = name;
            investorCapital = capital;
            shareOwned = new ArrayList<Share>();
            numberOwned = new ArrayList<Integer>();
        }

P.S。我删除了函数只是为了显示结构。然后我执行以下代码:

File investorData = new File("inv1.ser");

 if(investorData.exists()) {
            try {
                FileInputStream loadData = new FileInputStream(investorData);
                ObjectInputStream ois = new ObjectInputStream(loadData);
                inv1 = (Investor) ois.readObject();
                loadData.close();
                ois.close();
            } catch (Exception exc) {
                JOptionPane.showMessageDialog(null, "An error occurred", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
        else {
            try {
                String name = JOptionPane.showInputDialog(null, "What is your name?", "Creating new investor", JOptionPane.QUESTION_MESSAGE);
                double capital = Double.parseDouble(JOptionPane.showInputDialog(null, "What is your investor capital?", "Creating new investor", JOptionPane.QUESTION_MESSAGE));
                inv1 = new Investor(name, capital);
            }
            catch(NullPointerException exc) {
                JOptionPane.showMessageDialog(null, "You must enter details in order to proceed", "File Not Found", JOptionPane.ERROR_MESSAGE);
            }
            JOptionPane.showMessageDialog(null, "New investor " + inv1.getInvestorName() + " with balance: " + inv1.getInvestorCapital() + " has been successfully created!", "Investor Created", JOptionPane.INFORMATION_MESSAGE);
            try {
                FileOutputStream saveFile = new FileOutputStream(investorData);
                ObjectOutputStream oos = new ObjectOutputStream(saveFile);
                oos.writeObject(inv1);
                saveFile.close();
                oos.close();
            }
            catch(Exception exc) {
                JOptionPane.showMessageDialog(null, "An error occurred", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }

当我第一次启动程序时,它会提示我创建新的Investor对象,它成功完成并保存在适当的位置等等。之后,我可以使用程序:买入/卖出股票等,但一旦我关闭它,再次开放,它不会识别之前购买的股票。所以例如,当我关闭时,我有金 - 100,然后当我再次打开程序时它将显示金 - 100并将尝试再购买10,它将添加新的黄金对象以分享我将获得金 - 100,金 - 10.据我所知,它无法识别旧的Gold对象并添加了新的对象(如果它从未存在过,则是预期的。)

我无法上传整个程序,因为它很大,投资者类中有buyShare方法:

public void buyShare(double price, int amount, Share stock) {
        investorCapital -= price * amount;
        if(shareOwned.contains(stock)) {
            numberOwned.add(shareOwned.indexOf(stock), numberOwned.get(shareOwned.indexOf(stock)) + amount);
        }
        else {
            shareOwned.add(stock);
            numberOwned.add(amount);
        }
    }

分享课程(没有职能):

public class Share implements Serializable {

    private String shareName;
    private double shareValue;
    private int shareAvailable;
    private final double SHARE_PURE_VALUE;

    public Share(String name, double value, int available) {
        shareName = name;
        SHARE_PURE_VALUE = value;
        shareValue = value / available;
        shareValue = Math.round(shareValue * 10) / 10;
        shareAvailable = available;
    }

你可以看到我检查“包含”的位置它应该只返回添加numberOwned,而是创建新的,因此它找不到之前保存的以前的Gold。很抱歉解释得非常糟糕,如果方便的话,我可以发送一个程序。

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你希望得到&#34; Gold - 110&#34;,而不是&#34; Gold - 100,Gold - 10&#34;,对吧?

所以,我认为你必须在Share类上实现equals和hashCode方法。当&#34; shareOwned.contains(stock)&#34;被称为,永远不会返回真,对吗?

这就是为什么你的列表中总是有一个新的库存对象。

commons-lang3添加到您的项目中,并将此代码放在您的Share类上:

@Override
public boolean equals(Object obj) {
    return EqualsBuilder.reflectionEquals(this, obj);
}

@Override
public int hashCode() {
    return HashCodeBuilder.reflectionHashCode(this);
}

应该有效。

答案 1 :(得分:-1)

非常感谢你!我解决了这个问题。正如我所提到的,我必须覆盖Share类中的equals()和hashCode()方法,现在一切正常(似乎至少)完美。我试图使用循环来检查名称,但这个解决方案更简单!

@Override

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Share share = (Share) o;

        if (!shareName.equals(share.shareName)) return false;

        return true;
    }

    @Override
        public int hashCode() {
            return 1;
        }

        public void setMarketValue() {
            shareValue = SHARE_PURE_VALUE / shareAvailable;
            shareValue = Math.round(shareValue * 10) / 10;
        }