Java 将对象添加到ArrayList-获取上次找到对象的n倍
一、 我的项目有问题。当我从文件中读取游戏地图时,我试图将16个对象(建筑)添加到ArrayList。毕竟,我得到了16个相同的物体(最后一个在地图上找到的建筑物),我不知道为什么 在类游戏中创建ArrayList:Java 将对象添加到ArrayList-获取上次找到对象的n倍,java,arraylist,Java,Arraylist,一、 我的项目有问题。当我从文件中读取游戏地图时,我试图将16个对象(建筑)添加到ArrayList。毕竟,我得到了16个相同的物体(最后一个在地图上找到的建筑物),我不知道为什么 在类游戏中创建ArrayList: public static ArrayList<Building> buildingList = new ArrayList<>(); 而且它也不起作用 类映射中的方法: b.writeBuilding(); 当我将其写入NetBeans控制台时,它为我
public static ArrayList<Building> buildingList = new ArrayList<>();
而且它也不起作用
类映射中的方法:
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;
}
}
}
}
}
有什么想法吗?
提前谢谢你的帮助 建筑使用静态字段,每次初始化对象时都会覆盖这些字段,因此最终所有字段都是相等的。“任何想法”我假设他得到16个相同的对象,但期望16个不同的对象。我错了!很抱歉当然,我希望列表中有16个不同的对象。我有16个建筑在地图上,有不同的X和Y位置。和不同的ssX,ssY(图形)。但我得到了16倍的最后一次发现的建筑。它在地图上的同一个地方渲染了16次……如果在渲染时迭代列表,将建筑打印到
System.out
,会发生什么?使用(甚至覆盖)toString方法!这很可能是游戏渲染部分的缓冲错误…当我在将建筑添加到列表之前打印它时,它是可以的。加上全部后,有16个相同的对象。例如:我正在创建具有字段x:5y:5的项目,将其添加到列表中,在列表中,它具有x:2y:4。并且添加到列表中的每个对象在每个字段上都有相同的值……欢迎您。:)关于静态的一点是,它不是实例,而是类级别的变量,这意味着无论您创建了多少次新对象(new Buildink(…);),这些变量都将在每个实例之间共享。
for(Building b : Game.buildingList) {
field = ss.grabImage(b.getSSX(), b.getSSY(), 32, 32);
graphics.drawImage(field, b.getX()*32, b.getY()*32, null);
}
b.writeBuilding();
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);
}
}