繁体   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