如何使用C#创建单维,二维,三维以及多维数组?

时间:2016-04-20 19:29:03

标签: c# arrays combinations

我试图将1,2,3,4,5的所有组合(5C1,5C2,5C3,5C4,5C5)保持在单个阵列中。所以我需要在c#中使用for循环创建动态数组。

比如说, 这里n = 5,r = 1到5。 如果r = 1那么 我的数组将是单维数组,当r = 2时它将是二维数组,当r = 3然后是三维时,当r = 4然后是四维数组时它将持续到5的结尾。 我的代码如下:

string[] ShipArrayObj;
  public frmResult( string[] ShipArray )
    {
        InitializeComponent();           
        ShipArrayObj = ShipArray;
    }

    private void frmResult_Load(object sender, EventArgs e)
    {
        string[] arr = ShipArrayObj;           
        int n = ShipArrayObj.Count();
        for (int r = 1; r <= n; r++)
        {                
            StoreCombination(arr, n, r);
            richTextBox1.Text = richTextBox1.Text + "/";                
        }

    }

    void StoreCombination(string[] arr, int n, int r)
    {           
        string[] data = new string[r];            
        createCombination (arr, data, 0, n - 1, 0, r);
    }


   private void createCombination(string[] arr, string[] data, int start, int end, int index, int r)
    {
        if (index == r)
        {
            int j = 0;
            for (j = 0; j < r; j++)
             richTextBox1.Text = richTextBox1.Text + data[j].ToString();//Where I want to set array to keep combination values
             return;
        }

        int i = 0;
        for (i = start; i <= end && end - i + 1 >= r - index; i++)
        {
            data[index] = arr[i];
            CreateCombination(arr, data, i + 1, end, index + 1, r);
        }
    }

我将所有组合存储到Rich Text Box中,但希望保留在数组中。如果有人帮助我,那么我将感激你。

1 个答案:

答案 0 :(得分:2)

如果您习惯使用类似Java的东西,那么多维数组在C#中的语法略有不同。

Here's a page describing how to do them in C#.以下是该页面的摘录:

function wrapper<T>(parameter: T, foo: (p: T) => void) {
    foo(parameter);
}
const lambda = parameter => { // parameter is `any`
    parameter.a;
    parameter.b;
    //...
}
wrapper(first, lambda)

If you're interested in different combinations of things with a fixed number of them, something like this should be all you need.

If you're interested in different combinations of things with a dynamic number of them, something like this should be all you need.

(一般来说,除非您尝试优化效果,否则最好是可读/富有表现力。)

您可能需要考虑订单是否重要(无序集与有序列表)。我认为它不是来自你的代码(在这种情况下排序很好地消除&#34;重复&#34;),但我无法确定。

这是一个很好的例子,它易于阅读和修改变体,对于小数字来说并不是那么糟糕:

// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified.
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// A similar array with string elements.
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
                                        { "five", "six" } };

// Three-dimensional array.
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                 { { 7, 8, 9 }, { 10, 11, 12 } } };
// The same array with dimensions specified.
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                       { { 7, 8, 9 }, { 10, 11, 12 } } };

输出:

// -1, 0, ..., 5
var choices = Enumerable.Range(-1, 6); 

var possibleChoices = 
    from a in choices
    from b in choices
    from c in choices
    from d in choices
    from e in choices
    select (IEnumerable<int>)new [] { a, b, c, d, e };

// Remove -1's because they represent not being in the choice.
possibleChoices =
    possibleChoices.Select(c => c.Where(d => d >= 0));

// Remove choices that have non-unique digits.
possibleChoices =
    possibleChoices.Where(c => c.Distinct().Count() == c.Count());

// Sort the choices to indicate order doesn't matter
possibleChoices =
    possibleChoices.Select(c => c.OrderBy(d => d));

// Remove duplicates
possibleChoices = 
    possibleChoices.Select(c => new 
                           {
                               Key = string.Join(",", c),
                               Choice = c
                           }).
    GroupBy(c => c.Key).
    Select(g => g.FirstOrDefault().Choice);

foreach (var choice in possibleChoices) {
    Console.Out.WriteLine(string.Join(", ", choice));
}

这可能需要更加密集才能理解,对这种特定的组合变体进行硬编码并涉及递归但是更加通用/不是硬编码到0 1 2 3 4 0, 1 0, 2 0, 3 0, 4 1, 2 1, 3 1, 4 2, 3 2, 4 3, 4 0, 1, 2 0, 1, 3 0, 1, 4 0, 2, 3 0, 2, 4 0, 3, 4 1, 2, 3 1, 2, 4 1, 3, 4 2, 3, 4 0, 1, 2, 3 0, 1, 2, 4 0, 1, 3, 4 0, 2, 3, 4 1, 2, 3, 4 0, 1, 2, 3, 4 (并且{ {1}}在dotnetfiddle.net上而不是5)。它也完全懒惰/ 0.047s

0.094s

输出:

IEnumerable