簡體   English   中英

如何將數組添加到鋸齒狀數組中的數組中

[英]How to add an array into an array within a jagged array

我正在嘗試創建一個個人計算機程序,該程序可以讓我快速將配方添加到數據庫中,並可以按類別輕松查看。 我對該信息的基本設置是使鋸齒狀的類別數組組成,每個類別均具有該類別中的配方數組。 我一直在尋找解決方案,但沒有發現任何東西可以滿足我的特定需求。 下面基本上是我的數據的結構。

//Full List
string[][][][][] arrA = {
    //List Category
    string[][][][] arrB = {
        //Single Entry
        string[][][] arrC = {
            //Entry Subsection
            string[][] arrD = {
                //Entry Value
                string[] arrE = {
                    "foo", "bar"
                },
                //Entry Value
                string[] arrF = {
                    "bar", "foo"
                }
            }
        }
        //Add Array here
    }
    //List Category
    string[][][][] arrB = {
    //Single Entry
    //Etc...
    }
}

並有效地在上面Add Array Here地方Add Array Here

//Array to add (Entry in Category)
string[][][][] arrV = {
    string[][][] arrW = {
        string[][] arrX = {
            string[] arrY = {
                "rab", "oof"
            },
            string[] arrZ = {
                "oof", "rab"
            }
        }
    }
}

如果無法使用數組執行此操作,則我願意放入轉換器代碼,只要可以將其轉換回數組即可,因為我編寫的顯示代碼可用於這種鋸齒狀數組結構。

根據您定義的問題,我看不出為什么有這么多嵌套和多維數組是您選擇的數據結構的原因。 當然,麻煩多於其價值。

我建議使用從類別到該類別中的食譜陣列的映射。

class Recipe 
{
    public string name { get; set; }
    public List<string> ingredients { get; set; }
    // ... other things the class needs
}

class MyCookbook 
{
    public Dictionary<string, List<Recipe>> categories = new Dictionary();

    // Creating a single recipe and giving it ingredients
    var penne = new Recipe();
    penne.name = "Penne";
    penne.ingredients = new List<string>();
    penne.ingredients.add("Tomato Sauce");
    // ... add other ingredients

    // Creating a list of recipes we will associate with some category
    var typesOfPasta = new List<Recipe>();
    typesOfPasta.add(penne);

    // Putting that list of recipes into a category, this time "Pasta"
    categories.add("Pasta", typesOfPasta);
}

這是一個基本設置,您可以擴展該設置,使您可以擁有一個字符串(例如“ pasta”),該字符串指向一組食譜對象(例如“ lasagna”,“意大利面條和肉丸”等),其中每個食譜對象都包含該配方的必要成分列表。

int mat [][] = new int [5][];
int [] numbers = new int[5];      //{ v1, v2, v3, v5, v6};  Vn: variable

var arr1 = numbers.ToArray();

mat[0] = arr1;  //  o.K

mat[0] = numbers; // K.O

為了你的問題

嵌套List似乎可以解決它。 在以下方面:

var entireList = new List<List<List<List<List<string>>>>>();
var allCategories = new List<List<List<List<string>>>>(); 
and so on... 

那你可以做

entireList.Add(allCategories);
and so on...

我假設每個List的第一個元素都將List命名。 例如,對於“ Categories列表”,第一個元素將命名以下元素所屬的實際類別。

尋找解決方案

您打算如何將該列表存儲在數據庫中? 我建議您首先創建表和數據庫架構設計。 即類別表等。然后使用諸如EnityFramework之類的ORM連接到數據庫,然后根據需要訪問每個實體。 這樣做應該讓您擺脫以當前方式表示數據的痛苦。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM