Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# splitting up lists

rosin tuck
Greenhorn
Posts: 5
I have a list composed of lists of lists, looking this:
[[[hello, one, two, three],[four, to, the][six]],[[k][block, three, nine]],...
and so on.
What i need to do is seperate each individual list of lists, as below,
[[hello, one, two, three],[four, to, the],[six]]
[[k],[block, three, nine]]....

I then need to merge the individual lists inside thes lists together, like below:
[hello, one, two, three, four, to, the, six]
[k, block, three, nine]

I have written some code to iterate through the list, and find the inner list of list, which i want to merge together.

I dont however know what to do from here, i cannot find a way to store just the individual inner lists in seperate lists, so they can then be iterated through and their inside lists merged together. Does anybody know how to do this, or something to the same effect? I would greatly appreciate any help
Thanks

Purushoth Thambu
Ranch Hand
Posts: 425
What you need to implement is recursive calls with function returning the tokens. Let's say you have a list like below
[a,b,[abc,def],c,[ghi,[klmn,opq]]]

You need to do depth first traversal. So you will have something

The above function returns list of elements in a string with comma seperated list. From the returned string value you can split and convert it back to List.

rosin tuck
Greenhorn
Posts: 5
Thanks for the quick response! That is very close to what i am trying to do. The output now is the entire list of lists, combined together forming one list. What i need to do however is have a seperate list for each of the lists inside the outer list.
So using if your example contained another list like below:
[[a,b,[abc,def],c],[[ghi,[klmn,opq]]]]

it would return
[a,b,[abc,def],c]
[ghi,[klmn,opq]]

Do you know how to make this distinction between the lists?

Purushoth Thambu
Ranch Hand
Posts: 425
I tried the example as below:

The output is -
a,b,abc,def,c,ghi,klmn,opq,

suppose you want get the out like
(a,b,abc,def,c) and (ghi,klmn,opq)

then you have to call the splitList function with outer iterator.
something like

If this is not what you are expecting explain with an example what is the expected output.

rosin tuck
Greenhorn
Posts: 5
sorry im just trying to test that code but i cant get it to work. Not sure i follow the last bit you sent

Iterator itr = fin.iterator();
while(itr.hasNext())
{
Object obj = itr.next();
if (obj instanceof List)
System.out.println(splitList(fin,1));
else
System.out.println(obj.toString());
}

how come there is a 1 in the line splitList(fin,1)?

Purushoth Thambu
Ranch Hand
Posts: 425
Sorry about that, it's mistake remove the second parameter. It should work fine.