gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:41:14 25 4
gpt4 key购买 nike

我尝试将数组对象 (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com