繁体   English   中英

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

[英]How to create single dimensional, two dimensional,three dimensional as well as multi dimensional array using C#?

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

例如,这里n = 5且r = 1至5。如果r = 1则My数组将是一维数组,当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中,但要保留在数组中。 如果有人帮助我,我将感谢您。

如果您习惯使用Java之类的东西,那么多维数组的C#语法会有些不同。

这是描述如何在C#中执行这些操作的页面。 这是上述网页的摘录:

// 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

这可能需要更密集地理解,对组合的这种特定变体进行硬编码,并涉及递归,但更通用/不是硬编码为5 (在0.047s上花费0.047s而不是0.094s )。 它也完全是lazy / IEnumerable

public static void Main()
{       
    var possibleChoices = Choose(5);

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

public static IEnumerable<IEnumerable<int>> Choose(int max) 
{       
    var remaining = Enumerable.Range(0, max); 

    return ChooseRecursive(remaining, Enumerable.Empty<int>());
}

public static IEnumerable<IEnumerable<int>> ChooseRecursive(IEnumerable<int> remaining, IEnumerable<int> chosen) 
{       
    yield return chosen;

    foreach (var digit in remaining) 
    {
        var choices = ChooseRecursive(
            remaining.Where(d => d > digit), 
            chosen.Concat(new [] { digit })
        );
        foreach (var choice in choices)
        {                   
            yield return choice;
        }
    }
}

输出:

0
0, 1
0, 1, 2
0, 1, 2, 3
0, 1, 2, 3, 4
0, 1, 2, 4
0, 1, 3
0, 1, 3, 4
0, 1, 4
0, 2
0, 2, 3
0, 2, 3, 4
0, 2, 4
0, 3
0, 3, 4
0, 4
1
1, 2
1, 2, 3
1, 2, 3, 4
1, 2, 4
1, 3
1, 3, 4
1, 4
2
2, 3
2, 3, 4
2, 4
3
3, 4
4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM