java - 尝试存储变量的先前值然后更新,但它只是存储当前值? java

标签 java swing arraylist timer

我尝试将数组对象 (ao1) 的值存储在另一个数组列表对象 (ao2) 中,然后 ao1 的值发生更改。

我想比较这些值。

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

这是我的代码(我添加了我认为相关的内容,在本例中,ao1 将是“stockMarket”,ao2 将是“preStockMarket”):

StockGUI 类

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

            setVisible(true);
}

市场走势类

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 新手。

最佳答案

如果您想复制列表,请使用复制构造函数

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

关于java - 尝试存储变量的先前值然后更新,但它只是存储当前值? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42735583/

相关文章:

java - C 和 Python 代码到 Java 的帮助

java - 从/res/raw 中的文本文件填充 Android Fragment 中的 TextView

java - 限制类的直接实例数

java - 如何使用 OOP 概念使用 ArrayList 对象将值传递到数据库

java - IO异常 : When Executing Java File Code in Linux box

java - 使用 JTable 单元格编辑器

java - 创建后按指定顺序设置 JTable 的列

java - 方法无故关闭程序

java - 将字符串从数组列表转换为另一个字符串

Java,对构成有向图的节点 ArrayList 执行深拷贝