<?xml version="1.0"?>
<Event>
<Country>England</Country>
<City>London</City>
<City>Liverpool</City>
<Code>DWW</Code>
<ID>DG300</ID>
<ID>SS500</ID>
<Division>Europe</Division>
</Event>
Let say I have an XML like above. I have a method which takes different number of parameters:
myMethod(String... params){}
A parameter is an element which I want to read from XML. As an example lets take 3 elements form XML
myMethod(Country, City, ID){}
At the beginning, I count how many parameters is passed to this method:
int count= params.size(); // let say for this example count=3
Here I create an array with lists as an elements:
List<String>[] tab= new ArrayList[count];
Here I iterate as many times as count parameter is equal to and put a list in every array element:
for(int i=0; i<count; i++){
tab[i] = new ArrayList<>();
}
In the middle my method I have some loops which read elements from XML and put them to the arrays (add them to the lists). I use JAVAX
At the end my array looks like this
tab=[(England),(London,Liverpool),(DG300,SS500)]
Now I have a problem. I need Cartesian of every list which mean that i need lines:
England London DG300
England London SS500
England Liverpool DG300
England Liverpool SS500
I can do this with nested loops like this
for(int i=0; i< tab[0].size();i++){
for(int j=0; i< tab[1].size();j++){
for(int k=0; i< tab[2].size();k++){
System.out.println(tab[0].get(i)+ " " + tab[1].get(j)+" "+tab[2].get(k))
}}}}}
But this is not a good idea as I mentioned at the beginning, I can have a different number of parameters so I can have different number of nested loops. It can be two of them but can be ten as well.
How can replace this nested loops using RECURSION? Or maybe I can do this any other way using something else than recursion?
paramsis important here. Your question would be clearer if you madetabthe parameter to the method, and went from there.