简体   繁体   English

循环遍历多维数组

[英]Looping through multi dimensional array

Edit编辑

The approach taken here was completely wrong.这里采取的方法是完全错误的。 This is how it should have been done.这是应该如何完成的。

public class Recipe {
  public List<string> ingredients;
  public string result;

  public Recipe (List<string> _ingredients, string _result) {
    this.ingredients _ingredients;
    this.result = _result;
  }
}

And then when looping:然后在循环时:

Recipe[] recipes = new Recipe[] {
  new Recipe(new List<string>(new string[] { "potato", "tabasco" }), "explosivePotato"),
  new Recipe(new List<string>(new string[] { "mentos", "cola" }), "colaCannon"),
  new Recipe(new List<string>(new string[] { "potato", "cola" }), "potatoCola"),
  new Recipe(new List<string>(new string[] { "potato", "chips" }), "potatoChips")
}

This would also mean that looping would be a whole lot simpler.这也意味着循环会简单得多。


Original question原始问题

I have a C# script with the following array:我有一个包含以下数组的 C# 脚本:

string[,] craftingRecipes = new string[,]{
    // {recipe},{result}
    {"potato","tabasco"},{"explosivePotato"},
    {"mentos","cola"},{"colaCannon"},
    {"potato","cola"},{"potatoCola"},
    {"potato","chips"},{"potatoChips"}
};

How can i loop through every item in the n'th subarray?如何遍历第 n 个子数组中的每个项目? So for example the array containing {"potato","tabasco"} should be looped twice, since there are two items in it.因此,例如包含{"potato","tabasco"}的数组应该循环两次,因为其中有两个项目。
Thank you for any help!感谢您的任何帮助!

I'm not sure I understood your question, since your code doesn't compile, but you can use this loop to iterate through items in first "line":我不确定我是否理解您的问题,因为您的代码无法编译,但是您可以使用此循环来遍历第一“行”中的项目:

        int line = 0;
        for (int i = 0; i < craftingRecipes.GetLength(1); i++)
        {
            Console.WriteLine(craftingRecipes[line,i]);
        }

Although, Your array should look more like this (I had to add sth_missing_here , because Multi-Dimensional Array must have sub-arrays with the same length):虽然,您的array应该看起来更像这样(我必须添加sth_missing_here ,因为Multi-Dimensional Array必须具有相同长度的子数组):

        string[,] craftingRecipes = new string[,]{
            {"potato","tabasco"},{"explosivePotato","sth_missing_here_1"},
            {"mentos","cola"},{"colaCannon","sth_missing_here_2"},
            {"potato","cola"},{"potatoCola","sth_missing_here_3"},
            {"potato","chips"},{"potatoChips","sth_missing_here_3"}
        };

You can use the OfType<T> method, to convert the multidimensional array into an IEnumerable<T> result, for sample:您可以使用OfType<T>方法将多维数组转换为IEnumerable<T>结果,例如:

IEnumerable<string> items = craftingRecipes.OfType<int>();

foreach(var item in items)
{
   // loop between all elements...
}

As per the edit 19-09-2018, this can now be properly answered.根据 19-09-2018 的编辑,现在可以正确回答这个问题。

Iterate over the array using two simple nested foreach loops.使用两个简单的嵌套foreach循环foreach数组。

foreach (Recipe recipe in recipes) {
  System.Console.Write("To make '{0}' you need:", recipe.result);

  foreach (ingredient in recipe.ingredients) {
    System.Console.Write(ingredient);
  }
}

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

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