0

Could you tell me how can I put elements from a list to twodimensional array in C#?

tura = new string[howManyWords, 6];
    howManyWords = 0;

    foreach (words word in wc.words)
    {
        if (word.CategoryNumber == scb.Category)
        {
            tura[howManyWords] ......; howManyWords++;
        }
    }

I have a list made from XML file. I work in Unity.

   <?xml version="1.0" encoding="utf-8"?>
<WordCollection>
  <Words>
    <Word name="$mezczyzna">
      <CategoryNumber>1</CategoryNumber>
      <PolishName>mężczyzna</PolishName>
      <EnglishName>a man</EnglishName>
      <AudioName>man.mp3</AudioName>
      <ImageName>man</ImageName>
      <ImageLocalisationWidth>200</ImageLocalisationWidth>
      <ImageLocalisationHigh>700</ImageLocalisationHigh>
    </Word>
  </Words>
</WordCollection>
7
  • 3
    This doesn't look like a two dimensional array to me. This looks like a List of Objects of type Word. Why are you trying to make it a 2d array, and a 2d array of what? What are you expecting the new array to look like? Commented Nov 8, 2016 at 17:11
  • There is a built-in List.ToArray() method: msdn.microsoft.com/en-us/library/x303t819(v=vs.110).aspx that you may be able to use. Commented Nov 8, 2016 at 17:11
  • @dubstylee that is not a 2d array. Commented Nov 8, 2016 at 17:11
  • duplicate of stackoverflow.com/questions/11295746/… Commented Nov 8, 2016 at 17:13
  • 1
    I think the user wants a dictionary but doesn't know it. Commented Nov 8, 2016 at 17:14

1 Answer 1

1

Since we believe you mean by dictionary, and by the ContentNumber, here is an example:

void Main()
{
    List<Word> Words = new List<UserQuery.Word> { new Word { CategoryNumber = 0, EnglishName = "WORD1" }, new Word { CategoryNumber = 1, EnglishName = "WORD2" }, new Word { CategoryNumber = 1, EnglishName = "WORD3" } };

    Dictionary<CategoryTypes, List<Word>> categoryWords = Words.GroupBy(word => word.CategoryNumber).ToDictionary(x => (CategoryTypes)x.Key, x => x.ToList());

    if (categoryWords.ContainsKey(CategoryTypes.Category2))
    {
        List<Word> words = categoryWords[CategoryTypes.Category2];

        for (int i = 0; i < words.Count; i++)
        {
            Console.WriteLine(words[i].EnglishName);
        }
    }
}

class Word
{
    public int CategoryNumber { get; set; }
    public string EnglishName { get; set; }
}

enum CategoryTypes
{
    Category1 = 0,
    Category2 = 1
}

You can then call the dictionary categoryWords by the type of enum, and then only have your list of words.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.