0

Why I am getting duplicate entries in my ArrayList<String[]>?

  • allStepsJSONStringArray contains an array of single strings in the format of JSON
  • I loop through and pass each JSON string to a function that writes it to a temporary internal file
  • I read the file
  • Then pass it to getStepsArray() which breaks down the JSON string and puts each entry into a String[]

Loop to add to master ArrayList - allStepsArray

for (int i = 0; i < allStepsJSONStringArray.size(); i++) {
    writer.writeToInternal(allStepsJSONStringArray.get(i));
    reader.readFromInternal(writer.filename); 
    stepsArray = reader.getStepsArray();
    for (int s = 0; s < stepsArray.size(); s++) {
        allStepsArray.add(stepsArray.get(s));
    }
}

getStepsArray()

public ArrayList<String[]> getStepsArray() {
    try {
        JSONObject jObject = new JSONObject(jsonString);
        JSONArray jArray = jObject.getJSONArray("steps");
        String stepOrder = null;
        String stepName = null;
        String stepType = null;
        String stepId = null;
        String checklistId = null;
        String checklistName = null;

        for (int i = 0; i < jArray.length(); i++) {
            stepOrder = jArray.getJSONObject(i).getString("order");
            stepName = jArray.getJSONObject(i).getString("name");
            stepType = jArray.getJSONObject(i).getString("type");
            stepId = jArray.getJSONObject(i).getString("id");
            checklistId = jObject.getString("checklistId");
            checklistName = jObject.getString("checklistName");
            stepsArray.add(new String[] {stepOrder, stepName, stepType, stepId, checklistName, checklistId});
        }


    } catch (Exception e) {
        e.printStackTrace();
    }
    return stepsArray;
}
2
  • 3
    Because you don't seem to ever reset stepsArray. The second time you add elements to it, the previous elements will still be there and will get added to allStepsArray again. Commented Jan 8, 2014 at 3:09
  • @SotiriosDelimanolis You are correct! If you want some points, create an answer so I can accept it! Commented Jan 8, 2014 at 3:13

1 Answer 1

1

Word for word:

Because you don't seem to ever reset stepsArray. The second time you add elements to it, the previous elements will still be there and will get added to allStepsArray again.

Sign up to request clarification or add additional context in comments.

1 Comment

@TheNomad Don't worry about it, as long as you've solved your issue.

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.