2

I was trying to populate an arraylist to contain another arraylist but after the loop finishes adding the sub arraylist to the parent in the first round, in the next round it overwrites the previously added row and duplicates the value of 2nd round and so on; so finally i get an arraylist having duplicate rows all of them having values the last row in the cursor. To clarify it well... instead of returning something like this

       [Quote1, Location1]
       [Quote2, Location2]
       [Quote3, Location3]
       [Quote4, Location4]

it returns something like

       [Quote4, Location4]
       [Quote4, Location4]
       [Quote4, Location4]
       [Quote4, Location4]

Here is my code:

public ArrayList<ArrayList<String>> getAllVerses() {
    ArrayList<ArrayList<String>> allVersesAL = new ArrayList<ArrayList<String>>();
    ArrayList<String> subAL = new ArrayList<>();
    Cursor allVerse = getAllQuote(); //It's an outsider method
    allVerse.moveToFirst();
    do {
        //subAL.clear();
        subAL.add(allVerse.getString(allVerse.getColumnIndex(VERSE)));
        subAL.add(allVerse.getString(allVerse.getColumnIndex(LOC_AUTH)));
        Log.d("Content of subAL", subAL + "");
        allVersesAL.add(subAL);
    } while (allVerse.moveToNext());

    for (List<String> li : allVersesAL) {
        for (String s : li) {
            Log.d("getAllVerses", "Inside the second for loop...");
            Log.d("Testing arraylist...", s + " : ");
        }
    }
    return allVersesAL;
}

It assigns the data to the sub ArrayList from a cursor object. Please help!!! I spent a lot of time on this but couldn't figure out what's wrong with my code. Bless you all!

6
  • 1
    You seem to be adding lots of references to the same list subAl to your enclosing list allVersesAL. Instead of that, make a new inner list each time. Commented Aug 26, 2016 at 10:26
  • 1
    Uncomment this code: //subAL.clear(); will probably solved your problem Commented Aug 26, 2016 at 10:28
  • 1
    @ShaishavJogani No it won't. If he does that the list will only contain data for the last row in the cursor. Commented Aug 26, 2016 at 10:42
  • @KlasLindbäck I don't think so. Once subAl has been added to allVersesAL, there is no harm in clearing it and again add new values. It's same like create new instance of subAl Commented Aug 26, 2016 at 10:48
  • @ShaishavJogani Inserting does not create a copy of the object. If you clear subAl after adding it, you will clear the list that has been inserted (because it is the same object!). Commented Aug 26, 2016 at 10:58

2 Answers 2

3

When you insert a object in a List the actual object is inserted into the list. It isn't copied. If you do further operations on the same object it will affect the object stored in the list.

What you need to do is to create a new ArrayList object for each iteration of the outer loop of your population code.

do {
    //subAL.clear();
    subAl = new ArrayList<String>();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much!!! It took me more than half a day to figure out what happened...Verrrrrrrry helpful
1

Move ArrayList<String> subAL = new ArrayList<>(); inside the do while loop

2 Comments

Thank you very much. It helped a lot!
@Sammie please accept the answer and vote if it helped you. Thanks!

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.