在Java中,将状态和支出历史记录存储为列表的最佳方式是什么



我的存储历史记录的列表有问题。

基本上我有一个细胞矩阵,每个细胞都有状态、支出和历史。历史是一个包含可验证char statedouble payout的对象,但我在引用方面有很多问题。当我试图复制整个历史时,有一些我不知道如何处理的参考。

让我给你看代码(我知道把两个对象作为一个单元格复制的方式很愚蠢,但我一直在尝试各种可能的方法来消除这些引用(:

public class Cell {
public char state;
public double payout;

public class History{
public char state='E';
public double result;

public History (char state, double result)
{
this.state=state;
this.result=result;
}
public History (History history)
{
this.state=history.state;
this.result=history.result;
}
public char getState()
{
return state;
}
public double getResult()
{
return result;
}
}

public LinkedList<History> history;
public Cell (Settings settings)
{
this.state=settings.state;
this.payout=0;
history= new LinkedList<History>();
}
public void copyCell ( Cell dest, Cell temp)
{
dest.state=temp.state;
dest.payout=temp.payout;

dest.history= new LinkedList<History>();

for (int i =0; i<temp.history.size(); i++)
{
double result1=temp.history.get(i).result;
char state1=temp.history.get(i).state;

dest.history.add(new History (state1,result1));
}
}
}

我做错了什么?我想我不知道复制可验证性和引用对象之间的区别。有人能给我解释一下吗?

您应该研究momento模式。这是一个很好的教程来学习如何实现它以及它是什么

关于存储历史支出的最佳方式,我将执行以下操作:

您需要3个组件-timestateresult数量。

  1. 需要时间,这样您就可以按照自己喜欢的方式对历史进行排序,并可以按照指定的顺序从数据库加载它
  2. 我会使用linkedlist,因为它保持插入顺序
  3. 只需克隆整个列表,即可轻松克隆历史记录List<History> cloned = new LinkedList<>(oldList);

最新更新