How do you iterate over a list that has a "parent/child/grandChild/etc."?
Sample data:
{ID, Parent ID}
Object parent = {1, 0};
Object childA = {2, 1};
Object childB = {3, 1};
Object grandChildA = {4, 3};
Object grandChildB = {5, 2};
And the list would be, {parent, childA, childB, grandChildA, grandChildB}
How do you iterate it by "roots"?
Sample output:
- Parent
- ChildA
- GrandChildB
- ChildB
- GrandChildA
- ChildA
Thanks!
Sample data:
Constructor: SampleObject(int id,int parentId)
SampleObject parent = new SampleObject(1, 0);
SampleObject childA = new SampleObject(2, 1);
SampleObject childB = new SampleObject(3, 1);
SampleObject grandChildA = new SampleObject(4, 3);
SampleObject grandChildB = new SampleObject(5, 2);
I then placed the objects in an ArrayList: ArrayList testList
so, problem is, how to iterate over the list, so that the result would:
- Parent
- ChildA
- GrandChildB
- ChildB
- GrandChildA
- ChildA