Solved

All combinations from multiple arrays

Posted on 2004-04-12
6
1,032 Views
Last Modified: 2008-03-10
This shouldn't be too difficult, but... How can I get all possible combinations from multiple string arrays? The number of items in any array will be 1..n and number of arrays 1..n

E.g. first array could be:
word11
word12

second array:
word21
word22
word23

from these I should be able to get a new array:
word11 word21
word11 word22
word11 word23
word12 word21
word12 word22
word12 word23
0
Comment
Question by:mmiika
6 Comments
 
LVL 4

Expert Comment

by:Jarodtweiss
ID: 10804295
Not sure to understand... You want to loop on your arrays to get all the items ?

int i, j;

for (i = 0; i < myFirstArray.Count; ++i)
{
  for (j = 0; j < myFirstArray(i).MySecondArray.Count; ++j)
  {
    console.WriteLine(myFirstArray(i).MySecondArray(j).ToString());
  }
}
0
 
LVL 3

Author Comment

by:mmiika
ID: 10804382
This seems to work...

private ArrayList loopThroughArrays(ArrayList WordLists)
{
      ArrayList ret = new ArrayList();
      int wordCount = WordLists.Count;
      int lastWord = wordCount-1;
      int [] wordCursor = new int [wordCount];
      int [] wordCursorMaxValue =  new int [wordCount];
                  
      for(int i=0;i<wordCount;i++)
      {
            wordCursor[i] = 0;
            ArrayList thisList = (ArrayList)WordLists[i];
            wordCursorMaxValue[i] = thisList.Count-1;
      }
                  
      bool loop = true;
      while(loop)
      {
            string phrase = null;
            for(int iWord=0;iWord<wordCount;iWord++)
            {
                  ArrayList thisList = (ArrayList)WordLists[iWord];
                  string thisword = thisList[wordCursor[iWord]].ToString();
                  phrase += thisword + " ";
            }
            ret.Add( phrase.Trim() );
                        
            for(int iCursor=lastWord;iCursor>=0;iCursor--)
            {
                  if(iCursor==lastWord)
                        wordCursor[iCursor]++;
                  if(wordCursor[iCursor]>wordCursorMaxValue[iCursor])
                  {
                        wordCursor[iCursor]=0;
                        if(iCursor==0)
                              loop = false;
                        else
                              wordCursor[iCursor-1]++;
                  }
            }
      }

      return ret;
}
0
 
LVL 3

Author Comment

by:mmiika
ID: 10804386
I wanted to loop through unknown number of arrays...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Accepted Solution

by:
monosodiumg earned 50 total points
ID: 10805587
mmiika,

>The number of items in any array will be 1..n and number of arrays 1..n
You mean 1..n and 1..*m* or do you really mean that the number of items in each array is the same as the number of arrays?

A recursive version can be much tidier (this is air-code. Probably some syntax errors):
ArrayList function CrossProduct(ArrayList WordLists, string sRoot) {
      ArrayList OutList = new ArrayList();
      ArrayList Words = (ArrayList)WordLists[0];

      if (WordLists.Count == 1) {
            //Have reached last one so do not recurse.
                        
            foreach (Object  Word in Words) {
                  Outlist.Add sRoot + " " + (String)Word.trim();
            }
      } else {
            foreach (Object  Word in Words) {
                  Outlist.AddRange(CrossProduct(WordLists.GetRange(1,WordLists.Count - 1), sRoot + " " + (String)Word.trim();));
            }
      }
    return OutList ;
}

You call it with an empty string for the sRoot argument.

mono
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 10811104
When you want to loop through an unknown number of arrays, use StringCollection instead of array of strings and ArrayList instead of arrays. So you will have an ArrayList with StringCollections inside it, each StringCollection containing strings. Thus you can dynamically add strings and arrays and easily loop over them using the Count properties.
0
 
LVL 3

Author Comment

by:mmiika
ID: 11029045
monosodiumg, B-E-A-utiful, thanks...

Small modifications (a working function)

private ArrayList CrossProduct(ArrayList WordLists, string sRoot)
{
      ArrayList OutList = new ArrayList();
      ArrayList Words = (ArrayList)WordLists[0];

      if (WordLists.Count == 1)
      {
            //Have reached last one so do not recurse.
              foreach (Object  Word in Words)
                     OutList.Add(sRoot + " " + Word.ToString().Trim());
      }
      else
      {
            foreach (Object  Word in Words)
                  OutList.AddRange(CrossProduct(WordLists.GetRange(1,WordLists.Count - 1), sRoot + " " + Word.ToString().Trim()));
      }
      return OutList;
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now