1

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?

1 Answer 1

2

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<>();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.