add ArrayList to multiple ArrayList in java -
add ArrayList to multiple ArrayList in java -
hi have problem arraylists have 3 lists
arraylist1<integer>=[1,2,3] arraylist2<integer>=[] arraylist3<arraylist<integer>>=[]
arraylist1 elements used adding values arraylist2 example
for(int i:arraylist1) { for(int a=0;a<i;a++) { arraylist2.add(a); } }
and works fine no problem there want every element in arraylist1 add together arraylist2 arraylist3 have come not work
for(int i:arraylist1) { for(int a=0;a<i;a++) { arraylist2.add(a); } arraylist3.add(arraylist2); }
simply utilize addall
, , collections.fill
.
example
list2.addall(list1); list3 = new arraylist<arraylist<integer>>(list1.size()); collections.fill(list3, list2);
note list3
filled same instance of list2
.
this means every alter list2
reflected in each element of list3
.
if not behavior you're expecting, iterate on length of list1
, add together new arraylist<integer>(list2)
.
java
Comments
Post a Comment