将对象添加到ArrayList - 获得n次最后找到的对象

时间:2013-06-22 09:25:17

标签: java arraylist

我的项目出了问题。当我从文件中读取我的游戏地图时,我正在尝试向ArrayList添加16个对象(建筑物)。毕竟,我得到16个相同的对象(最后在地图上找到了建筑物),我不知道为什么......

在类游戏中创建ArrayList:

public static ArrayList<Building> buildingList = new ArrayList<>();

读取地图(类Map)[Terrain转到Array,如果找到则构建,转到ArrayList]:

private void readBuilding(Terrain objct, int x, int y) {
    System.out.format("Dodaję: ");
    //Czerwone centrum dowodzenia
    if(objct.getSSX() == 1 && objct.getSSY() == 3) {
        Building b = new Building(x, y, 1, 1);
        b.writeBuilding();
        Game.buildingList.add(b);
    }
    //Czerwona miasto
    if(objct.getSSX() == 2 && objct.getSSY() == 3) {
        Building b = new Building(x, y, 2, 1);
        b.writeBuilding();
        Game.buildingList.add(b);
    }
    //Czerwona fabryka
    if(objct.getSSX() == 3 && objct.getSSY() == 3) {
        Building b = new Building(x, y, 3, 1);
        b.writeBuilding();
        Game.buildingList.add(b);
    }
    //Niebieskie centrum dowodzenia
    if(objct.getSSX() == 1 && objct.getSSY() == 4) {
        Building b = new Building(x, y, 1, 2);
        b.writeBuilding();
        Game.buildingList.add(b);
    }
    //Niebieska fabryka
    if(objct.getSSX() == 2 && objct.getSSY() == 4) {
        Building b = new Building(x, y, 2, 2);
        b.writeBuilding();
        Game.buildingList.add(b);
    }
    //Niebieskie miasto
    if(objct.getSSX() == 3 && objct.getSSY() == 4) {
        Building b = new Building(x, y, 3, 2);
        b.writeBuilding();
        Game.buildingList.add(b);
    }
    //Niczyje miasto
    if(objct.getSSX() == 4 && objct.getSSY() == 3) {
        Building b = new Building(x, y, 3, 0);
        b.writeBuilding();
        Game.buildingList.add(b);
    }
    //Niczyja fabryka
    if(objct.getSSX() == 4 && objct.getSSY() == 4) {
        Building b = new Building(x, y, 2, 0);
        b.writeBuilding();
        Game.buildingList.add(b);
    }
}


private void createMap() {
    map = new Terrain[40][22];
    String[] fields = csvFileContent.split(";");
    int x, y;
    int fieldNo = 0;

    for(y = 0; y < 22; y++) {
        for(x = 0; x < 40; x++) {            
            Terrain objct = new Terrain();
            objct.setSSX(Integer.parseInt(fields[fieldNo].substring(0,2)));
            objct.setSSY(Integer.parseInt(fields[fieldNo].substring(2,4)));
            if(objct.getSSY() > 2) {
                readBuilding(objct, x, y);
                objct.setSSX(3);
                objct.setSSY(1);
            }
            fieldNo++;
            map[x][y] = objct;
        }
    }
    System.out.format("Elementow: %d\n",Game.buildingList.size());
    for(Building b : Game.buildingList) {
        b.writeBuilding();
    }
}

在屏幕上渲染地图(clas Map):

public void render(Graphics graphics, Game game) {
    int x, y;
    //System.out.println("Budynkow: " + game.getBuildingList().size());
    for(y = 0; y < 22; y++) {
        for(x = 0; x < 40; x++) {
            field = ss.grabImage(map[x][y].getSSX(), map[x][y].getSSY(), 32, 32);
            graphics.drawImage(field, x*32, y*32, null);
        }
    }
    Iterator<Building> it = Game.buildingList.iterator();
    while(it.hasNext()) { 
        Building b = it.next();
        field = ss.grabImage(b.getSSX(), b.getSSY(), 32, 32);
        graphics.drawImage(field, b.getX()*32, b.getY()*32, null);
    }
}

我也试图像这样迭代List:

for(Building b : Game.buildingList) {
    field = ss.grabImage(b.getSSX(), b.getSSY(), 32, 32);
    graphics.drawImage(field, b.getX()*32, b.getY()*32, null);
}

它也不起作用。

类Map中的方法:

b.writeBuilding();
当我将它写入NetBeans控制台时,

给了我正确的字段值,但它总是添加相同的值。

类地形:

package game;

class Terrain {
private int ssX;
private int ssY;
private int x;
private int y;
private boolean occupied;
private boolean crossable;

Terrain() {
    this.occupied = false;
}

public void setSSX(int ssX) {
    this.ssX = ssX;
}

public void setSSY(int ssY) {
    this.ssY = ssY;
}

public void setX(int x) {
    this.x = x;
}

public void setY(int y) {
    this.y = y;
}

public int getSSX() {
    return ssX;
}

public int getSSY() {
    return ssY;
}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public void setCrossable() {
    if(ssX == 2 && ssY == 2) {
        this.crossable = false;
    } else {
        if(ssX >= 6 && ssX <= 11) {
            if(ssY >= 1 && ssY <= 2) {
                this.crossable = false;
            } else {
                this.crossable = true;
            }
        }
    }
}
}

班级建设:

package game;

public class Building {
private static int owner;
private static int type;
private static int x;
private static int y;
private static int ssX;
private static int ssY;

public Building(int x, int y, int type, int owner) {
this.owner = owner;
this.type = type;
this.x = x;
this.y = y;

if(this.owner == 0) {
    if(this.type == 2) {
    this.ssX = 4;
    this.ssY = 4;
    } else if(this.type == 3) {
    this.ssX = 4;
    this.ssY = 3;
    }
} else if(this.owner == 1) {
    if(this.type == 1) {
    this.ssX = 1;
    this.ssY = 3;
    } else if(this.type == 2) {
    this.ssX = 2;
    this.ssY = 3;
    } else if(this.type == 3) {
    this.ssX = 3;
    this.ssY = 3;
    }
} else if(this.owner == 2) {
    if(this.type == 1) {
    this.ssX = 1;
    this.ssY = 4;
    } else if(this.type == 2) {
    this.ssX = 2;
    this.ssY = 4;
    } else if(this.type == 3) {
    this.ssX = 3;
    this.ssY = 4;
    }
}
}

public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSSX() {
return ssX;
}
public int getSSY() {
return ssY;
}

public void writeBuilding() {
System.out.format("Owner: %d Type: %d X: %d Y: %d SSX: %d SSY %d\n"
    ,this.owner,this.type,this.x,this.y,this.ssX,this.ssY);
}
}

有什么想法吗? 在此先感谢您的帮助。 ;)

1 个答案:

答案 0 :(得分:0)

构建使用静态字段,每次初始化对象时都会覆盖静态字段,所以最终所有字段都相等。