Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I have a
C#
List<List<int>>
with no idea of how many integers are there in each list or how many lists are there.

what type of loop could i use so that i get all the possibilities of the integers such that I get one integer from each List.

For example:
Here is the list of integers (each line representing a List of int and the entire thing representing the List of List of int)

1,3
2,4
3,5,6

all combinations are : (1,2,3), (1,2,5), (1,2,6), (1,4,3),(1,4,5),(1,4,6),(3,4,3),(3,4,5),(3,4,6),
(3,2,3),(3,2,5),(3,2,6) which i would like as the result

I prefer to get the combinations as a List of List of int.
Posted
Updated 12-Feb-14 4:52am
v3
Comments
rohith naik 12-Feb-14 11:36am    
I have solved this by using a very complex and bad algorithm. That is why I have not posted it here. If anyone does want it please let me know.

If it's just a list inside a list, why not use a double for loop?
C#
List<list><int>> mydoublelist = new List<list><int>>();
List<int> myintlist = new List<int>();

for(int i = 0; i < mydoublelist.Count; i++){
  List<int> mysinglelist = mydoublelist[i];
  for(int j = 0; j < mysingleist.Count; j++){
    myintlist.Add(mysinglelist[j]);
  }
}
</int></int></int></int></list></int></list>


Notes:
- I think there is an AddRange method wich would avoid the second loop. You could do something like myintlist.AddRange(mysinglelist); instead.
- If you need unique integers the AddRange won't work and you'll need the second loop. Check for the mysingelist.Contains( ... ) method.

hope this helps.
 
Share this answer
 
Comments
rohith naik 12-Feb-14 6:42am    
Honestly this makes no sense. I do not think you understood my problem.
V. 12-Feb-14 7:09am    
fair enough, but perhaps you can update your question with what you do want? (unless the answer was already given of course)
Here is a solution.
Add all elements in each list into a single list,then permute it.
C#
//Outer is your main list which contain other lists
foreach (List<int> i in Outer)NewList.AddRange(i);
</int>

Now permute NewList collection.
Here is a list of permutation articles in codeproject. use any1 of them.
 
Share this answer
 
Comments
rohith naik 12-Feb-14 10:50am    
we need only one element from a row. You method will yield ones with many elements from one row.
Silent Guardian 12-Feb-14 11:28am    
out of curiosity, what does your application do?
rohith naik 12-Feb-14 11:31am    
Basically I am using a mod in Solr to return the hit words back to me and trying to map the words back to the original text. Unfortunately This is as much info I can cram into a comment. This is kind of like a sub - sequence problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900