我有一个存储一些信息的课程。我还有这个类的一系列对象" Points []",所有信息都被正确添加,但是当我想获得其中一些" Points"的信息时,例如:
point[1].getId();
我有最后一个Id值,但我想要1值。即使" [i]"值是1或2或任何数字我只收到我之前添加的最后一个Id值。我把类代码和主代码更好地解释了。
点类:
public class Point implements java.io.Serializable {
private static final long serialVersionUID = -4910520794768384111L;
String _id;
String _comment;
String _calification;
String _coords;
int _X;
int _Y;
public Point(String id, String comment, String calification, String coords, int x, int y)
{
_id = id;
_comment = comment;
_calification = calification;
_coords = coords;
_X = x;
_Y = y;
}
public String getID()
{
return _id;
}
public String getComment()
{
return _comment;
}
public String getCalification()
{
return _calification;
}
public String getCoords()
{
return _coords;
}
public int getPointX()
{
return _X;
}
public int getPointY()
{
return _Y;
}
}
主类代码,这里是我需要获取所有信息的地方,我只收到我的Point类信息的最后一个对象:
for(int i = 0; i < points.length; i++)
{
try
{
js.put("id",points[i].getID());
js.put("calification",points[i].getCalification());
js.put("comment",points[i].getComment());
js.put("gps",points[i].getCoords());
js.put("X",points[i].getPointX());
js.put("Y",points[i].getPointY());
}
catch (JSONException e)
{
e.printStackTrace();
}
jsPoints.put(js);
}
我还在课堂上添加了一个值格式的图像。
有人可以帮我解决这个问题吗?我需要为&#34; i&#34;提供正确的价值。数字,不仅是最后一个。谢谢!
答案 0 :(得分:0)
我相信JS是一个hashmap。发生的事情是你将值放在带有特定键的hashmap中。
js.put("id",points[i].getID());
js.put("calification",points[i].getCalification());
js.put("comment",points[i].getComment());
js.put("gps",points[i].getCoords());
js.put("X",points[i].getPointX());
js.put("Y",points[i].getPointY());
在交互中,下一次迭代将覆盖前一次迭代保存的值。最后,您将只有hashmap中的最后一次迭代值。
以下是示例
js.put("id",1);
js.put("id",2);
js.put("id",3);
js.put("id",4);
这里将会发生的事情是,hashmap中只有一个值为4。
你应该使用其他一些statergy来保存这些值。