-1

AddAll is pretty cool, but adds it at the bottom of the list.

I want to add the sorted list A on top of the sorted list B. I need list A for a different view

listA = [1, 2, 3];
listB = [4, 5, 6];

listB.addAll(listA);
// lista == [4, 5, 6, 1, 2, 3]

The easiest but maybe bad solution i can think of is to add first listA then listB to a listC.

listC = [];
listC.addAll(listA);
listC.addAll(listB);
//listc == [1, 2, 3, 4, 5, 6]
listB = []
listB.addAll(listC)
// garbage collector to gets rid of C
// listB == [1, 2, 3, 4, 5, 6];
// listA == [1, 2, 3];

This seems a bit inefficient, so I was wondering if there is a better way for this that doesn't include loops or anything thelike. Something like flutters 'addAt', like listB.addAllAt(0, listA)

To clarify, having a third list is a 'necessary evil' that i would like to avoid. I want one list that would be AB, and one list that is just A, to go back to the question, how do i add A on top of B.

lrn
  • 64,680
  • 7
  • 105
  • 121
Maritn Ge
  • 997
  • 8
  • 35
  • Does this answer your question? [How do I combine two lists in Dart?](https://stackoverflow.com/questions/21826342/how-do-i-combine-two-lists-in-dart) – MendelG Jul 06 '22 at 15:25

1 Answers1

0

While the suggest answer did not solve my issue, it gave me the right idea. I can add them with 'plus' into one of the original lists

listA = [1, 2, 3];
listB = [4, 5, 6];

listB = listA+listB;

// listB == [1, 2, 3, 4, 5, 6];
// listA == [1, 2, 3];

Thank you MendelG for the right idea!

Maritn Ge
  • 997
  • 8
  • 35