简体   繁体   English

如何将数组添加到锯齿状数组中的数组中

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

I am trying to create a personal computer program that will let me quickly add recipes into a database and be easily viewed by category. 我正在尝试创建一个个人计算机程序,该程序可以让我快速将配方添加到数据库中,并可以按类别轻松查看。 My basic setup for the information is to have a jagged array of the categories, each having an array of the recipes in that category. 我对该信息的基本设置是使锯齿状的类别数组组成,每个类别均具有该类别中的配方数组。 I have searched for a solution, but found nothing that seemed to answer my specific need. 我一直在寻找解决方案,但没有发现任何东西可以满足我的特定需求。 Below is basically how my data is structured. 下面基本上是我的数据的结构。

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

And effectively what the array to add above where it says Add Array Here 并有效地在上面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"
            }
        }
    }
}

If it is not possible to do this with an array, I am willing to put in a converter code so long as it can be converted back to an array as my display code is written to work with this jagged array structure. 如果无法使用数组执行此操作,则我愿意放入转换器代码,只要可以将其转换回数组即可,因为我编写的显示代码可用于这种锯齿状数组结构。

Based on the problem you've defined I see no reason why this many nested and multidimensional arrays is your chosen data structure. 根据您定义的问题,我看不出为什么有这么多嵌套和多维数组是您选择的数据结构的原因。 It's certainly more trouble than it's worth. 当然,麻烦多于其价值。

I'd recommend using a mapping from category to the array of recipes in that category. 我建议使用从类别到该类别中的食谱阵列的映射。

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);
}

This is a basic setup that you can expand upon that would allow you to have a string (say "pasta") pointing to an array of Recipe objects (say "lasagna", "spaghetti and meatballs", etc) where each Recipe object contains a list of necessary ingredients for that recipe. 这是一个基本设置,您可以扩展该设置,使您可以拥有一个字符串(例如“ 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

For your problem 为了你的问题

It seems that nested List s could solve it. 嵌套List似乎可以解决它。 Something on the lines of: 在以下方面:

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

Then you could do 那你可以做

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

I presume that the first element of each List names the list. 我假设每个List的第一个元素都将List命名。 eg For List of Categories the first element would name the actual category to which the following elements belong to. 例如,对于“ Categories列表”,第一个元素将命名以下元素所属的实际类别。

For a solution 寻找解决方案

How do you plan to store this List in the database? 您打算如何将该列表存储在数据库中? I recommend that you create tables and database schema design first. 我建议您首先创建表和数据库架构设计。 ie A table for categories, etc. Then use an ORM like EnityFramework to connect to the database and then access each entity as required. 即类别表等。然后使用诸如EnityFramework之类的ORM连接到数据库,然后根据需要访问每个实体。 Doing this should relive you of the pain of representing the data in the way your are currently doing. 这样做应该让您摆脱以当前方式表示数据的痛苦。

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

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