如何绕过Java中的ConcurrentModificationException?

时间:2018-04-25 20:35:56

标签: java arrays arraylist concurrentmodification

这是个人项目,我试图用恐龙作为代理人来创建一个类似生存的模拟。

模拟的主要部分是do:while循环,内部执行:while循环为3:每个循环(不是嵌套)运行三个不同的ArrayLists。每个ArrayList都是一个对象数组,每个对象都做不同的事情。

Dinosaurs在字段中移动,可以吃其他Dinosaur个对象,Grass个对象或Water个对象。在被吃掉之后,Grass个对象和Water个对象都不会消失,但Dinosaur dinosaurs个对象如果被吃掉,则会从ArrayList for (Grass grass : grasses) { System.out.println(grass); } //simulation loop do { for (Grass grass : grasses) { grass.timeToGrow(); } for (Water water : waters) { } for (Dinosaur dinosaur : dinosaurs) { if(simulationLength % 3 == 0) { //dinosaurs lose 1 health and food every 3 turns dinosaur.addFood(-1); dinosaur.addWater(-1); } dinosaur.loseHealth(); dinosaur.move(dinosaurs, grasses, waters); dinosaurs.add(dinosaur.timeToReproduce()); } System.out.println("There are " + dinosaurs.size() + " dinosaurs left."); simulationLength -= 1; simulationTime += 1; //TODO update GUI } while (simulationLength > 0); System.out.println("¡THE SIMULATION IS DONE!"); 中删除。这是我的相关代码:

dinosaur.move()

dinosaur.timeToReproduce()将每个恐龙移动到恐龙的ArrayList中。如果恐龙是一种食肉动物并登陆另一只恐龙,它会吃掉它,并且吃掉的恐龙会从名单中删除。如果恐龙应该使用食物并生成另一个Dinosaur(已添加到Dinosaur列表中),则Dinosaurs会返回public void move(ArrayList<Dinosaur> dinosaurs, ArrayList<Grass> grasses, ArrayList<Water> waters) { Positioned food = getFoodSource(dinosaurs, grasses, waters); try { //to find food if(distanceTo(food) <= getSpeed()) { //can move to food in one turn moveInRange(dinosaurs, grasses, waters); } else { //food is out of reach moveOutRange(dinosaurs, grasses, waters); } } catch (NullPointerException e) { //didn't find any food :( //TODO make the dino search for alternate food source //TODO make the dino wander if truly no food //not yet moving setxLoc(getxLoc()); setyLoc(getyLoc()); } } public void moveInRange(ArrayList<Dinosaur> dinosaurs, ArrayList<Grass> grasses, ArrayList<Water> waters) { Positioned food = getFoodSource(dinosaurs, grasses, waters); //searching out target... int xLocFood = food.getxLoc(); int yLocFood = food.getyLoc(); this.setxLoc(xLocFood); this.setyLoc(yLocFood); switch (food.getName()) { case "grass": //grass just got eaten. it now has to regrow System.out.println(this + " just ate " + food + "!"); grasses.get(grasses.indexOf(food)).setGrowthStage(1); this.addHealth(4); this.addFood(4); break; case "water": //you gots to do nothing. water doesn't go away System.out.println(this + " just drank " + food + "!"); this.addHealth(2); this.addWater(4); break; case "dinosaur": //remove the dinosaur you just ate System.out.println(this + " just tried to eat " + food + "!"); battle(dinosaurs.get(dinosaurs.indexOf(food)), dinosaurs); break; } } public void moveOutRange(ArrayList<Dinosaur> dinosaurs, ArrayList<Grass> grasses, ArrayList<Water> waters) { Positioned food = getFoodSource(dinosaurs, grasses, waters); //searching out target... //if there is not a valid food source found then the next 2 lines will throw a NullPointerException int xLocFood = food.getxLoc(); int yLocFood = food.getyLoc(); int newXLoc; int newYLoc; double dx = xLocFood - getxLoc(); //total x distance to food double dy = yLocFood - getyLoc(); //total y distance to food double theta = Math.atan(dy / dx); //θ = arctan(dy/dx) //rounding for accuracy newXLoc = (int) Math.round(getSpeed() * Math.cos(theta)); //x=speed*cos(θ) newYLoc = (int) Math.round(getSpeed() * Math.sin(theta)); //y=speed*sin(θ) setxLoc(getxLoc() + newXLoc); //newX = x+moveX setyLoc(getyLoc() + newYLoc); //newY = y+moveY } public void battle(Dinosaur dino, ArrayList<Dinosaur> dinosaurs) { //attack vs defense int dH = this.getAttack() - dino.getDefense(); if(dH > 0) { //dinosaur's attack is greater than the other guy's defense System.out.println("\t" + dino + " just lost " + dH + " health!"); dino.addHealth(-dH); this.addFood(dino.curFood / 2); if(dino.getHealth() <= 0) { System.out.println("\t" + dino + " was just eaten!"); for (int i = 0; i < dinosaurs.size(); i++) { if(dinosaurs.get(i).equals(dino)) { dinosaurs.remove(i); } } } } else if(dH < 0) { //defender has the big defense System.out.println("\t" + this + " just lost " + dH + " health!"); this.addHealth(dH); badDinos.add(dino); } } 个对象,如果不是,则返回null。

Iterator

我见过ArrayList的一些内容,我应该使用什么?如果是这样,他们如何运作?我应该在move() / battle()timeToReproduce()中创建新的dinosaurs,然后在完成df61.head(3) mpe_wgt pic_code 10 420336479305589843900801597032 10 420907139300189843900792911982 10 420967449300189843900797682603 mpe_wgt object pic_code object df_petsy_gz.head(3) monthly_fiscal_year month pic_code class_of_mail 2017 11 420606019300189843900566128707 FC 2017 11 420731629300189843900584700299 FC 2017 11 420405029300189843900568579224 FC weight calc_postage calc_total_postage MikeZone 0.8750 4.02 4.02 5 0.3750 2.77 2.77 6 0.6875 3.60 3.60 8 monthly_fiscal_year int64 month int64 pic_code object class_of_mail object weight float64 calc_postage float64 calc_total_postage float64 MikeZone int64 df61_mpe=pd.merge(df_petsy_gz,df61,on='pic_code', how='outer') 循环后删除内容吗?如果是这样,我如何确保应该被吃掉的恐龙呢?

随着人们的要求,我会不断更新细节。

0 个答案:

没有答案