试图从变量中存储先前的值然后更新,但它只是存储当前值? Java的

时间:2017-03-11 13:09:39

标签: java swing arraylist timer

我试图将数组对象(ao1)的值存储在另一个arraylist对象(ao2)中,然后更改ao1的值。

我想比较这些值。

但是,ao2(应该具有旧值)仍然最终具有像ao1这样的新值。为了使这更复杂一点,ao2每隔5秒通过一个计时器类随机改变。对于一些上下文,这是为了模拟股票市场。为了进一步复杂化,它涉及使用Swing GUI。

这是我的代码(我已经添加了我认为相关的内容,在这种情况下,ao1将是&#34; stockMarket&#34;而ao2将是&#34; preStockMarket&#34;):< / p>

StockGUI类

public StockGUI(){
    timer.schedule(new TimerTask()
                {
                    @Override
                    public void run() {
                        displayCurrentStock(stockMarket, moveMarket);
                    }
                }, 0, 5000);

            setVisible(true);
}

MarketMovement Class

private Random rn1 = new Random();
    //to determine whether to make the market move in a positive direction (1) or negative (0)
    private int rand1;

    private ArrayList<Stock> preStockList = new ArrayList();
    //create a timer so that I can replicate constant market movement
    java.util.Timer timer = new java.util.Timer();

    public MarketMovement(Market market){

        getChangedMarket(market);
    }

    public void alterMarket(Market market){
        ArrayList<Stock> listOfStocks = market.getStockList();
        for (Stock stock : listOfStocks){
            rand1 = rn1.nextInt(2);

            if (rand1 == 1){
                market.pipUp(stock.getStockName().toString());
                System.out.println("current Stock is up " + stock.getStockName());
            }
            else {
                market.pipDown(stock.getStockName().toString());
                System.out.println("current Stock is down " + stock.getStockName());
            }
        }
    }
    public void storePreStock(Market market){
        for (Stock stock : market.getStockList()){
            preStockList.add(stock);
        }
    }
    public void getChangedMarket(Market stockMarket){
        //This timer allows me to run a randomiser constantly to determine which direction the market should move
        timer.schedule(new TimerTask()
            {
                @Override
                public void run() {
                    storePreStock(stockMarket);
                    //rand1 = rn1.nextInt(2);
                    alterMarket(stockMarket);
                }
            }, 0, 5000);
    }
    public ArrayList<Stock> getPreStockList(){
        return preStockList;
    }

市场类

    private Stock stock1;
    private Stock stock2;
    private Stock stock3;

    private ArrayList<Stock> stockList = new ArrayList();

    int[] possibility = {0,1,1,1,1,1,2,2,2,2,3,3,3,4,4,5,5,5,6,6,7,8,9,9,10,11,12,1,1,1,2,2,2,3,3,3};
    private int rnd = new Random().nextInt(possibility.length);
    /**
     * Constructor for objects of class Market
     */
    public Market()
    {
        // initialise instance variables
        stock1 = new Stock("GBP/USD", 1.2245, 1.2244);
        stock2 = new Stock("GBP/EUR", 1.3342, 1.3341);
        stock3 = new Stock("EUR/USD", 1.0224, 1.0223);
        stockList.add(stock1);
        stockList.add(stock2);
        stockList.add(stock3);

    }

    public void pipUp(String stockname){
                //Loop through each item in the ArrayList "stockList" and change its value based on the input created from MarketMovement.alterMarket()
        for (Stock stock : stockList){
            if (stock.getStockName().equals(stockname)){
                //change the increase value by a random number
                double currentBuy = stock.getBuy();
                double currentSell = stock.getSell();

                stock.setBuy(round(currentBuy + (0.0001*possibility[rnd]), 4));
                stock.setSell(round(currentSell + (0.0001*possibility[rnd]), 4));
            }
        }
    }

    public void pipDown(String stockname){
                for (Stock stock : stockList){
            if (stock.getStockName().equals(stockname)){
                //change the increase value by a random number
                double currentBuy = stock.getBuy();
                double currentSell = stock.getSell();

                stock.setBuy(round(currentBuy - (0.0001*possibility[rnd]), 4));
                stock.setSell(round(currentSell - (0.0001*possibility[rnd]), 4));
            }
        }
    }

也许当我将stockMarket中的值分配给preStockMarket变量时,它只是引用地址而不是实际存储的值,因此无论如何总会有新值?

道歉,我还是Java的新手。

1 个答案:

答案 0 :(得分:1)

如果要复制List,请使用复制构造函数

List<Integer> oldList = new ArrayList<>();
List<Integer> newList = new ArrayList<>(oldList);