Skip to main content
gamedev.net
🔒 Locked

[.net] array list combinations

Started by softengg Aug 10, 2007 at 9:01 AM 1 replies 1k views
Original Post
softengg
softengg
Halo, I am having a big problem with arraylist/array item combinations. This is not a home work question. I need it very urgent and I tried out some method which is not useful. The problem is that I have a array as follows: Array = {"A", "Sd", Fg", "X", ....may contain n items} How to find out the possible combination between these items? The expected answer would be ASdFgX, XSdFgA, SdXAFg, etc. I tried out so many methods. But being a beginner in C#, it is hard to find out. Please help me. I am using C#.Net2 Win Apps. I tried out following method using an array list....... ArrayList list = new ArrayList(); list.Add("A"); list.Add("Sd"); list.Add("Fg"); list.Add("X"); int count = 0; for (int i = 0; i < list.Count; i++) { lst1.Items.Add(list.ToString()); for (int j = 0; j < list.Count; j++) { if (j != i) lst1.Items.Add(list.ToString() + list[j].ToString()); for (int k = 0; k < list.Count; k++) { if ((k != i) && (k != j) && (j != i)) lst1.Items.Add(list.ToString() + list[j].ToString() + list[k].ToString()); for (int l = 0; l < list.Count; l++) { if ((k != i) && (k != j) && (j != i) && (l != k) && (l != j) && (l != i)) { lst1.Items.Add(list.ToString() + list[j].ToString() + list[k].ToString() + list[l].ToString()); } } } } } (Here lst1 is the ListBox) This is good for if we have 3 or 4 items in the array. But what to do if there is more than 10 items in the array?? pls help me. Thanks in advance
benryves
benryves
Recursion can make this easier to think about.

I've written a very stupid, but easy-ish to follow method. What I do is keep track of two lists of strings: one of the current combination being built up, and one of the bits of the string that are left to add.

void FindCombinations(List<string> current, List<string> available) {	if (available.Count == 0) {		// We have no more terms left, so this is a valid combination!		// (substitute in your lst1.Items.Add method).		Console.WriteLine(string.Join("", current.ToArray()));	} else {		// Loop through each of the "available" strings:		foreach (string ToAppend in available) {			// Create a copy of "current" and "available"			List<string> NewCurrent = new List<string>(current.ToArray());			List<string> NewAvailable = new List<string>(available.ToArray());			// Add the current string to the new list and remove it from those which are available:			NewCurrent.Add(ToAppend);			NewAvailable.Remove(ToAppend);			// Continue until there are no more combinations available.			FindCombinations(NewCurrent, NewAvailable);		}	}}


To use it, kick off with an empty list for "current":

List<string> list = new List<string>(new string[] { "A", "Sd", "Fg", "X" });FindCombinations(new List<string>(), list);


I probably haven't explained myself very well, so ask if you have any questions. [smile]
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]
SnOrfys
SnOrfys
alternatively, a non-recursive method would be:

        static void Main(string[] args)        {            List<String> list = new List<String>();            list.Add("A");            list.Add("Sd");            list.Add("Fg");            list.Add("X");            foreach (String s in FindCombinations(list))                Console.WriteLine(s);        }                public static List<String> FindCombinations(List<String> list)        {	        List<String> combinations = new List<String>();	        String combined;	        for (Int32 i = 0; i < list.Count; i++)	        {                combined = list;		        for(Int32 j = 0; j < list.Count; j++)		        {			        if(i != j)			        {				        combined += list[j];			        }		        }		        combinations.Add(combined);	        }            return combinations;        }


I also think the point should be made, even though it's not a problem here, ArrayLists are pretty slow and the generic List is preferred.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.