我有一个Location类,它存储多个位置对象,从JSON文件解析。目前,我可以使用toString()方法打印列表中的所有位置。输出如下:
http://hastebin.com/eruxateduz.vhdl
这是列表中某个位置的示例:
Location [location=null, id=3, description=You are at battlefield of Jerusalem. You are in awe of your surroundings, you see an exit to the west., weight=100, name=Jerusalem, exit=[Exit [title=Hattin, direction=West]]],
但我想知道如何通过字段值id打印出该列表中的特定位置,例如id =“2”的位置?
我认为可以创建一个覆盖toString方法来获取id的输入,并打印相应的位置对象,但不知道如何通过'id'打印:
if(LocationObject.getId() == "2") {
//do something
}
这是存储对象的POJO Location类:
public class Location {
private Location[] location;
private int id;
private String description;
private String weight;
private String name;
private Exit[] exit;
private boolean visited = false;
private boolean goalLocation;
private int approximateDistanceFromGoal = 0;
private Location parent;
public Location[] getLocation() {
return location;
}
public void setLocation(Location[] location) {
this.location = location;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription ()
{
return description;
}
public void setDescription (String description)
{
this.description = description;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public String getName ()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
public Exit[] getExit() {
return exit;
}
public void setExit(Exit[] exit) {
this.exit = exit;
}
@Override
public String toString() {
return "Location [location=" + Arrays.toString(location) + ", id=" + id
+ ", description=" + description + ", weight=" + weight
+ ", name=" + name + ", exit=" + Arrays.toString(exit)
+"]";
}
}
答案 0 :(得分:1)
尝试检查ID
if(LocationObject.getId() == "2") {
System.out.println(LocationObject.getDescription());
}