如何通过对象数组列表中的字段 id 打印特定对象?
How to print a specific object by field id within an array list of objects?
我有一个存储多个位置对象的位置 class,从 JSON 文件解析。目前,我可以使用 toString() 方法打印出列表中的 all 个位置。输出如下所示:
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 位置 class:
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)
+"]";
}
}
尝试检查 id
if(LocationObject.getId() == "2") {
System.out.println(LocationObject.getDescription());
}
我有一个存储多个位置对象的位置 class,从 JSON 文件解析。目前,我可以使用 toString() 方法打印出列表中的 all 个位置。输出如下所示:
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 位置 class:
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)
+"]";
}
}
尝试检查 id
if(LocationObject.getId() == "2") {
System.out.println(LocationObject.getDescription());
}