所以我有得到一些座標從一個ArrayList SQL數據庫,並將其存儲功能。我在從光標讀取值時打印這些值,它們很好。但是,當我立即循環並打印出存儲在數組列表中的值時,所有值似乎都設置爲我添加到數組列表中的最後一個值。我在這裏做錯了什麼?ArrayList.add()工作不正常
代碼:
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
謝謝你做的伎倆,我沒有意識到數組列表使用指針來分配座標。你有什麼建議在Java指針的來源?謝謝 – jgelderloos
嗯,它更多的是學習指針如何工作以及Java如何隱藏它們。 Java是完全傳遞值的。下面是一個關於C語言指針的網站:http://www.cprogramming.com/tutorial/lesson6.html –
我在C語言中有指針經驗,我只是沒有意識到java正在這樣做,因爲它並沒有讓它變得明顯。正在這樣做。 – jgelderloos