簡體   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