2

What the difference between assigning an arraylist to another and using method addAll between two arraylists?

1 > arrayList = arrayList; //should assign value of later arrayList to the first.

2> arrayList.addAll(arrayList) //add all the data of the later list to first.

the first completely replaces the data in the list ? the second one for appending data in the list(if it already has any) ???

if i do arrayList.add(arrayList) without assigning any data to the first list, will it insert any data ?

I did the following code for testing and found results that i do'not really know.

secondList.add("1");
secondList.add("2");

firstList = secondList;
Log.i("CHECK","first list =  "+ firstList);

firstList.addAll(secondList);
Log.i("CHECK","Firs list add : "+firstList);

firstList.clear();
firstList.addAll(secondList);
Log.i("CHECK","Firs list add 2 : "+firstList);

Result were :

CHECK: first list =  [1, 2]
CHECK: Firs list add : [1, 2, 1, 2]
CHECK: Firs list add 2 : []

i was expecting the last log to have result like : [1,2]

as mentioned in docs.oracle.com

addAll- Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.

and if there's no data in the list ? then what will addAll DO ?

0

3 Answers 3

9

When you do:

firstList = secondList;

What you are saying is actually "to make firstList and secondList refer to the same list". After the line is executed, there will only be one list and two variables both refer to that list.

This is why after you cleared firstList, secondList lost all the elements as well. They refer to the same thing. This has nothing to do with addAll. When you called firstList.addAll(secondList), you are basically adding appending an empty list to another empty list, which results in an empty list.

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

Comments

5

when you use arrayList = arrayList2; then you are assigning the reference of arrayList2 in first list. That means they are referring to the same list.

and when you use arrayList.addAll(arrayList2) then they are two different list reference.

Now come back to your code (lets denote firstlist as f, second as s)

        secondList.add("1"); // f={}, s = {1}
        secondList.add("2"); // f={}, s = {1,2}

        firstList = secondList; // f= s = {1, 2}
        Log.i("CHECK","first list =  "+ firstList); // so printing 1,2

        firstList.addAll(secondList);// it is actually adding itself.. so f= s = {1,2,1,2}
        Log.i("CHECK","Firs list add : "+firstList);

        firstList.clear(); // clear boths as s = f
        firstList.addAll(secondList); // as boths are blank so overall blank
        Log.i("CHECK","Firs list add 2 : "+firstList);

1 Comment

The comments within you code help in better understanding.
1

I learned about this in class, Java doesnt really specify when it passes by value or passes by reference, but for the sake of arrayList's, they are pass by reference unless you specifically create new elements. When you say

firstArray = secondArray;

firstArray gets the memory address of the second array, therefore when you cleared the first array, you actually cleared the memory which the second array also shares.

Good luck!

1 Comment

Not memory adress, but link to the object

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.