简体   繁体   中英

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

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

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. 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. eg For List of Categories the first element would name the actual category to which the following elements belong to.

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. Doing this should relive you of the pain of representing the data in the way your are currently doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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