Java 无法获取我自己类的对象列表的正确值
我有一个类存储一些信息。我还有一个此类Points[]的对象数组,所有信息都已正确添加,但当我想获得其中一些点的信息时,例如:Java 无法获取我自己类的对象列表的正确值,java,android,list,class,object,Java,Android,List,Class,Object,我有一个类存储一些信息。我还有一个此类Points[]的对象数组,所有信息都已正确添加,但当我想获得其中一些点的信息时,例如: point[1].getId(); 我有最后一个Id值,但我想要1值。即使[i]值是1或2或任何数字,我也只接收我之前添加的最后一个Id值。我把类代码和主代码放在一起,以便更好地解释它 积分等级: public class Point implements java.io.Serializable { private static final long s
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;
}
}
主类代码,这里是我需要所有信息的地方,我只收到我的点类信息的最后一个对象:
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);
}
我还在类中添加了一个值格式的图像
有人能帮我解决那个问题吗?我需要的是I号的正确值,而不仅仅是最后一个。谢谢 我相信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来保存这些值。我认为这不是问题,因为我将js放在JSONArray上:jsPoints.putjs;当我看到JSONArray上的值时,它们总是相同的,例如,我有3个不同值的点,JSONArray上的第三个值是相同值的三倍,因为js每次都接收相同的最后一个值,即使数字是0或1或2。我添加这部分代码是为了帮助您理解我。