Reputation: 43
I am using an ArrayList to track the path of a line drawn on the screen (Android).
I would like to save these values (in another ArrayList), then analyse them once I hit the "Submit" button.
However, it seems to be adding a pointer and not saving what is there at that particular time.
public static ArrayList<Float> drawnData = new ArrayList<>();
public static ArrayList<ArrayList<Float>> saveData = new ArrayList<>();
//This is called from another class upon MotionEvent.ACTION_UP
//drawnData is updated from another class during the drawing
public void updateData(){
saveData.add(drawnData);
}
How do I set this up so it saves what is in drawnData every time the method is called, and not just a pointer to what is currently in drawnData?
For example, if this method is called 5 times, when I go to look at the data, it contains 5 ArrayLists, all containing data for the path that was last drawn, instead of each of the individual paths.
Bonus question: Should I just be using List instead of ArrayList?
Upvotes: 1
Views: 107
Reputation: 393781
Add a copy of drawnData
:
saveData.add(new ArrayList<Float>(drawnData));
As for List vs ArrayList, it's always better to declare the variables using the interface types instead of the implementation types.
public static List<Float> drawnData = new ArrayList<>();
public static List<List<Float>> saveData = new ArrayList<>();
Upvotes: 2