gpt4 book ai didi

java - ArrayList.add() 无法正常工作

转载 作者:行者123 更新时间:2023-12-01 06:31:07 26 4
gpt4 key购买 nike

所以我有一个函数可以从 SQL 数据库获取一些坐标并将它们存储在数组列表中。当我从光标读取值时,我正在打印这些值,它们很好。但是,当我在循环并打印出存储在数组列表中的值后立即,所有值似乎都设置为我添加到数组列表中的最后一个值。我在这里做错了什么吗?

代码:

public ArrayList<Coord> getCoordMarkers(int myUserId)
{
Log.d("getCoordMarkers()", "Called");
Coord myCoord = new Coord();
ArrayList<Coord> markerArray = new ArrayList<Coord>();
String sql = "SELECT "+ userId + " , " + timestamp + " , " + coordX + " , " +
coordY + " , " + coordType + " , " + coordId + " , " + coordTypeTable+ "." +
coordTypeDesc + " FROM " + coordTable + " JOIN " + coordTypeTable + " ON " +
coordTable + "." + coordType + " = " + coordTypeTable + "." + coordTypeAbbr +
" WHERE " + userId + " = '" + myUserId + "' AND " + coordTypeTable + "." +
coordTypeDesc + " != 'User Location';";
// AND it is a marker
SQLiteDatabase db = this.getWritableDatabase();
Cursor cur = db.rawQuery(sql, null);
if( cur.moveToFirst()) {
do {
myCoord.userId = cur.getInt(0);
myCoord.timestamp = cur.getLong(1);
myCoord.x = cur.getDouble(2);
myCoord.y = cur.getDouble(3);
myCoord.coordType = cur.getInt(4);
myCoord.id = cur.getInt(5);
myCoord.coordTypeDesc = cur.getString(6);
markerArray.add(myCoord);
Log.d("getCoordMarkers()", "X: " + myCoord.x + " Y: " + myCoord.y);
} while( cur.moveToNext());
}
cur.close();
db.close();
for( int i = 0; i < markerArray.size(); i++ )
{
Coord myCoord2 = markerArray.get(i);
Log.d("Test Marker", "I: "+ i + " X: " + myCoord2.x + " Y: "+ myCoord2.y);

}
return markerArray;
}

以下是上述代码的日志打印输出

06-27 01:46:06.588: DEBUG/getCoordMarkers()(541): Called
06-27 01:46:06.638: DEBUG/getCoordMarkers()(541): X: -122.093451 Y: 37.420599
06-27 01:46:06.638: DEBUG/getCoordMarkers()(541): X: -122.093451 Y: 37.420599
06-27 01:46:06.648: DEBUG/getCoordMarkers()(541): X: -122.094695 Y: 37.419679
06-27 01:46:06.648: DEBUG/getCoordMarkers()(541): X: -122.094695 Y: 37.419679
06-27 01:46:06.648: DEBUG/getCoordMarkers()(541): X: -122.090104 Y: 37.423633
06-27 01:46:06.658: DEBUG/getCoordMarkers()(541): X: -122.090104 Y: 37.423633
06-27 01:46:06.658: DEBUG/getCoordMarkers()(541): X: -122.092078 Y: 37.420464
06-27 01:46:06.658: DEBUG/getCoordMarkers()(541): X: -122.092078 Y: 37.420464
06-27 01:46:06.658: DEBUG/getCoordMarkers()(541): X: -122.091992 Y: 37.420668
06-27 01:46:06.668: DEBUG/getCoordMarkers()(541): X: -122.091992 Y: 37.420668
06-27 01:46:06.668: DEBUG/getCoordMarkers()(541): X: -122.09594 Y: 37.42077
06-27 01:46:06.678: DEBUG/getCoordMarkers()(541): X: -122.09594 Y: 37.42077
06-27 01:46:06.678: DEBUG/getCoordMarkers()(541): X: -122.09255 Y: 37.42118
06-27 01:46:06.678: DEBUG/getCoordMarkers()(541): X: -122.09255 Y: 37.42118
06-27 01:46:06.688: DEBUG/getCoordMarkers()(541): X: -122.095897 Y: 37.423633
06-27 01:46:06.698: DEBUG/getCoordMarkers()(541): X: -122.095897 Y: 37.423633
06-27 01:46:06.708: DEBUG/Test Marker(541): I: 0 X: -122.095897 Y: 37.423633
06-27 01:46:06.708: DEBUG/Test Marker(541): I: 1 X: -122.095897 Y: 37.423633
06-27 01:46:06.708: DEBUG/Test Marker(541): I: 2 X: -122.095897 Y: 37.423633
06-27 01:46:06.708: DEBUG/Test Marker(541): I: 3 X: -122.095897 Y: 37.423633
06-27 01:46:06.718: DEBUG/Test Marker(541): I: 4 X: -122.095897 Y: 37.423633
06-27 01:46:06.718: DEBUG/Test Marker(541): I: 5 X: -122.095897 Y: 37.423633
06-27 01:46:06.728: DEBUG/Test Marker(541): I: 6 X: -122.095897 Y: 37.423633
06-27 01:46:06.739: DEBUG/Test Marker(541): I: 7 X: -122.095897 Y: 37.423633
06-27 01:46:06.739: DEBUG/Test Marker(541): I: 8 X: -122.095897 Y: 37.423633
06-27 01:46:06.739: DEBUG/Test Marker(541): I: 9 X: -122.095897 Y: 37.423633
06-27 01:46:06.748: DEBUG/Test Marker(541): I: 10 X: -122.095897 Y: 37.423633
06-27 01:46:06.748: DEBUG/Test Marker(541): I: 11 X: -122.095897 Y: 37.423633
06-27 01:46:06.748: DEBUG/Test Marker(541): I: 12 X: -122.095897 Y: 37.423633
06-27 01:46:06.748: DEBUG/Test Marker(541): I: 13 X: -122.095897 Y: 37.423633
06-27 01:46:06.758: DEBUG/Test Marker(541): I: 14 X: -122.095897 Y: 37.423633
06-27 01:46:06.758: DEBUG/Test Marker(541): I: 15 X: -122.095897 Y: 37.423633

最佳答案

尝试重新实例化 myCord。这看起来像是一个简单的重新分配问题。了解指针是值得的,即使是使用 Java!

    do {
myCoord = new Coord(); //**********
myCoord.userId = cur.getInt(0);
myCoord.timestamp = cur.getLong(1);
myCoord.x = cur.getDouble(2);
myCoord.y = cur.getDouble(3);
myCoord.coordType = cur.getInt(4);
myCoord.id = cur.getInt(5);
myCoord.coordTypeDesc = cur.getString(6);
markerArray.add(myCoord);
Log.d("getCoordMarkers()", "X: " + myCoord.x + " Y: " + myCoord.y);
myCoord = null; //for the garbage collector *******
} while( cur.moveToNext());

关于java - ArrayList.add() 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11218439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com