簡體   English   中英

將 state 的歷史記錄和支出作為列表存儲在 Java 中的最佳方式是什么

[英]What is the best way to store history of state and payout as a list in Java

我的存儲歷史記錄的列表有問題。

基本上我有單元格矩陣,每個單元格都有 state、支出和歷史記錄。 歷史記錄是 object,其中包含驗證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));
        }
    }
}

我做錯了什么? 我想我不知道復制 veriables 和引用 object 之間的區別。有人可以解釋一下嗎?

你應該看看momento模式。 是一個很好的教程,可以學習如何實現它以及它是什么。

關於存儲歷史支出的最佳方式,我會執行以下操作:

您需要 3 個組成部分 - timestateresult金額。

  1. 需要時間,以便您可以按照自己喜歡的方式對歷史進行排序,並可以按照指定的順序從數據庫中加載它。
  2. 我會使用鏈表,因為它保持插入順序
  3. 克隆歷史很容易,只需克隆整個列表List<History> cloned = new LinkedList<>(oldList);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM