如何解决在处理中填充对象数组-所有对象都相同
我向您保证,我已经花了数小时试图在互联网上找到解决方案,但是我迫切需要一双新的眼睛。我目前正在使用ArrayLists,但是尝试使用普通的对象数组,并且发生了相同的问题。就上下文而言,我试图模拟一种流行病,并试图在社区边界内以随机位置填充“ Person”类型的数组。目前的Person类如下:
private PVector pos;
class Person{
public Person(float x,float y){
pos = new PVector(x,y);
}
void show(){
ellipse(pos.x,pos.y,10,10);
}
float xPos(){
return pos.x;
}
}
我的社区课程如下:
private float size;
private PVector origin;
private ArrayList<Person> personArr;
class Community{
public Community(float x,float y,float size_){
origin = new PVector(x,y);
size = size_;
personArr = new ArrayList<Person>();
}
void show(){
noFill();
rect(origin.x,origin.y,size,size);
showPersons();
}
void setupCommunity(){
for(int i = 0; i < initialPopulation; i++){
float x1 = random(origin.x,origin.x + size);
float y1 = random(origin.y,origin.y + size);
Person person = new Person(x1,y1);
personArr.add(person);
}
}
void showPersons(){
for(int i = 0; i < personArr.size(); i++){
personArr.get(i).show();
}
}
}
针对社区的show()方法在我的主Simulation类中的draw()方法中的每一帧都被调用一次,到目前为止,作为参考,它看起来像这样:
Grapher graphInfected,graphSuseptible,graphRemoved;
Community community;
float graphLength = 250;
float communitySize;
int initialPopulation = 25,population;
int populationInfected = 2,populationSusceptible = 23,populationRemoved = 0;
public void settings(){
size(1700,1000);
communitySize = height * 0.8;
}
void setup(){
population = initialPopulation;
community = new Community(150,100,communitySize);
community.setupCommunity();
graphInfected = new Grapher(width/2 + 240 + 200,height/2 - 230,"Infected",graphLength);
graphSuseptible = new Grapher(width/2 + 240 + 200,height/2 + 90,"Suseptible",graphLength);
graphRemoved = new Grapher(width/2 + 240 + 200,height/2 + 400,"Removed",graphLength);
}
void draw(){
background(255);
community.show();
graphInfected.addToArray(populationInfected);
graphSuseptible.addToArray(populationSusceptible);
graphRemoved.addToArray(populationRemoved);
graphInfected.show();
graphSuseptible.show();
graphRemoved.show();
}
这个想法是将所有人显示在社区矩形内的Persons数组中,这看起来像1个人,因为他们都被绘制在同一位置。我知道这一点是因为在调试时,我看到在社区设置中添加到personArr时,随机位置是不同的,但是每次我添加到数组列表时,整个列表中都填充了完全相同的Person。这样就形成了由创建的最后一个人组成的整个列表。如果有人知道原因,我将不胜感激!我只需要用个人Person对象填充列表!谢谢
如果您想尝试运行该项目,以下是绘图仪的代码:
class Grapher {
private IntList population;
private int countArr = 1,dataLength = 0;
private PVector[] linePos;
private PVector origin;
private String graphType = "";
private float length;
private String yLable = "";
Grapher(int x,int y,String graphType,float length){
origin = new PVector(x,y);
population = new IntList();
this.graphType = graphType;
this.length = length;
population.set(0,initialPopulation);
}
//Called every every frame
void show() {
dataLength = population.size();
linePos = new PVector[dataLength];
//background(255,255,255);
int largestPop = initialPopulation;
for (int i = 0; i < dataLength; i++) {
if (population.get(i) > largestPop) {
largestPop = population.get(i);
}
}
//UI code
stroke(0,0);
line(origin.x,origin.x + (int) length,origin.y);
fill(0);
textSize(15);
text("" + largestPop,origin.x - 60,origin.y - length);
text("" + dataLength,origin.x + length,origin.y + 25);
fill(0);
textSize(15);
text("Time",(float) (origin.x + length/2 - length/10),origin.y + 30);
text(yLable,origin.x - 100,(float) (origin.y - length/2));
//Calculating the graph points
line(origin.x,origin.x,origin.y - (int) length);
double yInterval = length/(largestPop);
double interval = length/dataLength;
for (int i = 0; i < dataLength; i++) {
float xPos = origin.x + (float) interval * i;
float yPos = origin.y - ((float) yInterval * (population.get(i)));
linePos[i] = new PVector(xPos,yPos);
//ellipse(xPos,yPos,5,5);
}
//Picking the graph colour
if(graphType.equalsIgnoreCase("Infected")){
stroke(255,0);
yLable = "Infected";
}else if(graphType.equalsIgnoreCase("Susceptible")){
stroke(0,255);
yLable = "Susceptible";
}else{
stroke(0,0);
yLable = "Removed";
}
//Drawing the graph and connecting the points
for (int i = 0; i < dataLength - 1; i++) {
line(linePos[i].x,linePos[i].y,linePos[i + 1].x,linePos[i + 1].y);
}
}
void addToArray(int population){
this.population.set(countArr,population);
countArr++;
}
}
解决方法
PVector pos放在Person类之外。因此,所有Person对象都使用相同的pos。
与“社区”类相同。如果您创建多个社区,可能会导致奇怪的错误。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。