Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图存储变量中以前的值,然后进行更新,但它';它只是存储当前值?JAVA_Java_Swing_Arraylist_Timer - Fatal编程技术网

试图存储变量中以前的值,然后进行更新,但它';它只是存储当前值?JAVA

试图存储变量中以前的值,然后进行更新,但它';它只是存储当前值?JAVA,java,swing,arraylist,timer,Java,Swing,Arraylist,Timer,我试图将一个数组对象(ao1)的值存储在另一个arraylist对象(ao2)中,然后修改ao1的值 然后我想比较这些值 然而,ao2(应该有旧的值)仍然会有像ao1一样的新值。为了使这更复杂一点,ao2每5秒通过一个定时器类随机改变一次。在一点上下文中,这是为了模拟股票市场。对于更复杂的问题,它涉及到Swing GUI的使用 这是我的代码(我添加了我认为相关的内容,在本例中,ao1将是“股票市场”,ao2将是“预股票市场”): StockGUI类 市场运动班 private Random rn

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

然后我想比较这些值

然而,ao2(应该有旧的值)仍然会有像ao1一样的新值。为了使这更复杂一点,ao2每5秒通过一个定时器类随机改变一次。在一点上下文中,这是为了模拟股票市场。对于更复杂的问题,它涉及到Swing GUI的使用

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

StockGUI类 市场运动班
private Random rn1=new Random();
//确定是否使市场朝着积极方向(1)或消极方向(0)移动
私人机构1;
private ArrayList preStockList=new ArrayList();
//创建一个计时器,这样我就可以复制不断的市场运动
java.util.Timer Timer=新的java.util.Timer();
公共市场运动(市场){
getChangedMarket(市场);
}
公众街市(街市){
ArrayList listOfStocks=market.getStockList();
用于(库存:库存清单){
rand1=rn1.nextInt(2);
if(rand1==1){
market.piup(stock.getStockName().toString());
System.out.println(“当前库存上升”+Stock.getStockName());
}
否则{
market.pipDown(stock.getStockName().toString());
System.out.println(“当前股票下跌”+Stock.getStockName());
}
}
}
公共仓库预库存(市场){
for(Stock:market.getStockList()){
预库存清单。添加(库存);
}
}
公开无效getChangedMarket(市场股票市场){
//这个计时器允许我不断运行随机器,以确定市场应该向哪个方向移动
timer.schedule(新TimerTask()
{
@凌驾
公开募捐{
storePreStock(股票市场);
//rand1=rn1.nextInt(2);
证券市场;
}
}, 0, 5000);
}
公共ArrayList getPreStockList(){
返回预库存列表;
}
市场等级
私人股本1;
私人股本2;
私人股本3;
private ArrayList stockList=new ArrayList();
int[]可能性={0,1,1,1,1,1,2,2,2,3,3,4,4,5,5,5,6,7,8,9,9,10,11,12,1,1,1,2,2,3,3};
private int rnd=new Random().nextInt(可能性.length);
/**
*类市场对象的构造函数
*/
公开市场()
{
//初始化实例变量
股票1=新股(“英镑/美元”,1.2245,1.2244);
股票2=新股(“英镑/欧元”,1.3342,1.3341);
股票3=新股票(“欧元/美元”,1.0224,1.0223);
库存清单。添加(库存1);
库存清单。添加(库存2);
存货清单。添加(存货3);
}
公共void piup(字符串stockname){
//循环遍历ArrayList“stockList”中的每个项目,并根据MarketMovement.alterMarket()创建的输入更改其值
用于(库存:库存清单){
if(stock.getStockName().equals(stockname)){
//将增加值更改为随机数
double currentBuy=stock.getBuy();
double currentSell=stock.getSell();
股票.setBuy(四舍五入(当前买入+(0.0001*可能性[rnd]),4);
股票集合销售(四舍五入(当前卖出+(0.0001*可能性[rnd]),4);
}
}
}
公共void下拉列表(字符串stockname){
用于(库存:库存清单){
if(stock.getStockName().equals(stockname)){
//将增加值更改为随机数
double currentBuy=stock.getBuy();
double currentSell=stock.getSell();
股票.setBuy(四舍五入(当前买入-(0.0001*可能性[rnd]),4);
股票集合销售(四舍五入(当前卖出-(0.0001*可能性[rnd]),4);
}
}
}

也许当我将stockMarket中的值分配给preStockMarket变量时,它只是引用了地址而不是实际存储的值,因此不管怎样都会有新的值


抱歉,我还是Java新手。

如果要复制
列表
请使用复制构造函数

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

欢迎使用堆栈溢出!看起来您需要学习使用调试器。请随便吃点。如果您以后仍然有问题,请随时回来提供更多详细信息。我甚至不知道这个存在!我想这已经成功了-谢谢!
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));
            }
        }
    }
List<Integer> oldList = new ArrayList<>();
List<Integer> newList = new ArrayList<>(oldList);