0

I have been trying to get my head around this but it hasn't been working:

I've gotten this function from http://fir3pho3nixx.blogspot.com/2011/01/recursion-cross-product-of-multiple.html where it returns a list but I can't seem to read the values within each object in the list.

Here is the function in question:

    private static List<object> GetCrossProduct(object[][] arrays)
    {
        var results = new List<object>();
        GetCrossProduct(results, arrays, 0, new object[arrays.Length]);
        return results;
    }

    private static void GetCrossProduct(ICollection<object> results, object[][] arrays, int depth, object[] current)
    {
        for (var i = 0; i < arrays[depth].Length; i++)
        {
            current[depth] = arrays[depth][i];
            if (depth < arrays.Length - 1)
                GetCrossProduct(results, arrays, depth + 1, current);
            else
                results.Add(current.ToList());
        }
    }
5
  • 1
    Why would it return something your function has no return type and neither an out parameter.. it will always return as new List<object>() Commented Jul 4, 2011 at 5:11
  • Can you be more clear? What is the problem? Are you calling GetCrossProduct()? What argument are you passing it? Commented Jul 4, 2011 at 5:14
  • It's a recursive function when you need to return a cross product of a string[][] array. Commented Jul 4, 2011 at 5:14
  • The list returns what appears of a list of objects (and each object has the strings I need) ex: dl.dropbox.com/u/49200/List_Object.png I am having a lot of trouble pulling the values within the object (John, Red, Apple) Commented Jul 4, 2011 at 5:17
  • what is the purpose of passing current on the recursive call? looks like it should hold all elements of array[depth], isn't it? Commented Jul 4, 2011 at 5:32

1 Answer 1

1

You are having problem because you are probably expecting a linear List, when it is actually a List of Lists.

To access elements within your result, you need to do something like this:

var resultingList = GetCrossProduct(blargh); // where blargh is the array you passed in
foreach (IList<object> innerList in resultingList)
{
    foreach (var listValue in innerList)
    {
        // listValues should be the individual strings, do whatever with them
        // e.g.
        Console.Out.WriteLine(listValue);
    }
}

The reason for this is because of the line:

results.Add(current.ToList());

Which creates a new list and adds it to the result list.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow thanks so much, I see that now. The recursive function is adding the results in a new list within the list that's being returned. Thanks again for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.