[英]What is the best way to store history of state and payout as a list in Java
我的存儲歷史記錄的列表有問題。
基本上我有單元格矩陣,每個單元格都有 state、支出和歷史記錄。 歷史記錄是 object,其中包含驗證char state
和double 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));
}
}
}
我做錯了什么? 我想我不知道復制 veriables 和引用 object 之間的區別。有人可以解釋一下嗎?
關於存儲歷史支出的最佳方式,我會執行以下操作:
您需要 3 個組成部分 - time
、 state
和result
金額。
List<History> cloned = new LinkedList<>(oldList);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.