所有对象都等于最后一个对象的ArrayList

时间:2018-11-29 13:22:41

标签: java object arraylist

我遇到了几天来一直试图解决的问题。问题是,当我最后创建对象的ArrayList时,它们都等于最后一个对象。我相信这与不断添加相同的对象有关,但是据我所知,我每次都创建一个新对象。

该程序的重​​点应该是制造一堆不同的单元格或用于模拟目的(人),这些单元随后可以根据其随机属性相互交互。

这是我遇到的代码

private ArrayList<Cell> cellsList = new ArrayList<Cell>(); //This appears before the Class is created
private int startingCellNumber = 10; //This appears before the Class is created

//This is inside the class
//Go through and create the starting cells
for(int index=0; index < startingCellNumber; index++) {
  cellsList.add(new Cell());
}

任何帮助将不胜感激,我知道在Stack Overflow上存在与此问题类似的问题,但是所有这些问题的答案都不能解决我的问题。

两个完整代码(用Java处理编写) 跑步者

private ArrayList<Cell> cellsList = new ArrayList<Cell>();
private int startingCellNumber = 10;

void setup() {
  //Set up the background stuff
  size(600,400);
  frameRate(30);
  noStroke();


  //Go through and create the starting cells
  for(int index=0; index < startingCellNumber; index++) {
    //Cell newCell = new Cell();
    cellsList.add(new Cell());
    System.out.println(index + " : " + cellsList.get(index).getColonyColor() + " : (" + cellsList.get(index).getLocation(0) + "," + cellsList.get(index).getLocation(1) + ")"); //Here to test the output
  }

  System.out.println("------------------");

  //Test to see where error lies
  for(int index=0; index < startingCellNumber; index++) {
    System.out.println(index + " : " + cellsList.get(index).getColonyColor() + " : (" + cellsList.get(index).getLocation(0) + "," + cellsList.get(index).getLocation(1) + ")");
  }
}
void draw() {
  //Refresh the background
  background(51);
  //Go through and draw all of the cells (will be moved to the draw function)
  for(Cell activeCell : cellsList) {
    activeCell.drawCell();
    //System.out.println(activeCell);
  }
}

细胞类

private int strength;
private int age;
private int reproduction;
private String colonyColor;
private boolean disease;
private int[] location = {0,0};

private String[] directions = {"LEFT","Right","Up","Down"};

class Cell {
  //This is the constructor for the cell class 
  Cell() {
    strength = (int)(Math.random()*8)+3;
    age = 0;
    reproduction = (int)(Math.random()*6)+0;
    colonyColor = pickColor();
    if(colonyColor.equals("BLUE")) { location[0] = (int)(Math.random()*591)+0; location[1] = (int)(Math.random()*391)+0; }
    else if(colonyColor.equals("RED")) { location[0] = (int)(Math.random()*591)+0; location[1] = (int)(Math.random()*391)+0; }
  }
  //This class will randomly pick a team color for the cell
  private String pickColor() {
    int randColorNum = (int)(Math.random()*2)+1;
    if(randColorNum == 1) { return "BLUE"; }
    else if(randColorNum == 2) { return "RED"; }
    else { return "BLUE"; }
  }
  //This method picks the direction in which the cell will move
  private void moveDirection() {
    String chosenDirection = directions[(int)(Math.random()*4)+0];

    if(chosenDirection.equals("Left")) { checkNewLocation(-1,0); }
  }
  //this method uses the direction given by moveDirection() and decides what to do with it
  private void checkNewLocation(int xChange,int yChange) {

  }

  public void drawCell() {
    //Changes color depending on cells colonyColor
    if (getColonyColor().equals("RED")) { fill(255,0,0); }
    else { fill(0,0,255); }
    //Should draw the cell
    //System.out.println(getLocation(0) + " " + getLocation(1));
    rect(getLocation(0), getLocation(1), 10, 10);
  }

  //All of the gets and set methods for the Cell class's variables
  public int getStrength() { return strength; }
  public void setStrength(int s) { strength = s; }
  public int getAge() { return age; }
  public void setAge(int a) { age = a; }
  public int getReproduction() { return reproduction; }
  public void setReproduction(int r) { reproduction = r; }
  public String getColonyColor() { return colonyColor; }
  public void setColonyColor(String c) { colonyColor = c; }
  public boolean getDisease() { return disease; }
  public void setDisease(boolean d) { disease = d; }
  public int getLocation(int index) { return location[index]; }
}

1 个答案:

答案 0 :(得分:0)

我的代码的问题是我在class Cell { }行之前创建了所有Cell类变量。通过将它们移动到行后,一切正常。