java - difference between arraylist = arraylist and arraylist.addAll(arraylist) -
what difference between assigning arraylist , using method addall between 2 arraylists?
1 > arraylist = arraylist;
//should assign value of later arraylist first.
2> arraylist.addall(arraylist)
//add data of later list first.
the first replaces data in list ? second 1 appending data in list(if has any) ???
if arraylist.add(arraylist) without assigning data first list, insert data ?
i did following code testing , found results do'not 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 :
check: first list = [1, 2] check: firs list add : [1, 2, 1, 2] check: firs list add 2 : []
i expecting last log have result : [1,2]
as mentioned in docs.oracle.com
addall- appends of elements in specified collection end of list, in order returned specified collection's iterator.
and if there's no data in list ? addall ?
when do:
firstlist = secondlist;
what saying "to make firstlist
, secondlist
refer same list". after line executed, there 1 list , 2 variables both refer list.
this why after cleared firstlist
, secondlist
lost elements well. refer same thing. has nothing addall
. when called firstlist.addall(secondlist)
, adding appending empty list empty list, results in empty list.
Comments
Post a Comment