Home > Enterprise >  How does foreach work on a list with 2d arrays as elements?
How does foreach work on a list with 2d arrays as elements?

Time:11-11

In my Unity project I have a List with 2d string arrays as elements inside of that list:

List<string[,]> listOfLists = new List<string[,]>()

//the array has a variable amount of rows and 3 columns

It basically holds some arrays that I need to save. Now, to control if everything is working, I want to put it in a foreach-loop and output everything that was saved, but it just returns the 3rd column of the array saved in listOfLists[0]

    foreach(string smth in listOfLists[0])
    {
        return smth;
    }

I put that foreach-loop inside override .ToString() (Debug.Log(xxx.ToString())) and I know it should work returning a string[,] as string but it does and it returns the 1st column, so if the array is defined like this:

listOfLists[0][0, 0] = "A";
listOfLists[0][0, 1] = "B";
listOfLists[0][0, 2] = "C";

listOfLists[0][1, 0] = "D";
listOfLists[0][1, 1] = "E";
listOfLists[0][1, 2] = "F";

it JUST returns A and NOT B, C, D, E or F.

I can output it like this, so the array isn't not working or anything:

return $"{listOfLists[0][0, 0]} geht in die {listOfLists[0][0, 2]} und ihr Foto ist aufzufinden bei: {listOfLists[0][0, 1]}";

I just think its more practical to put it in a foreach-loop, than to call everything by it's own.

I know I did something wrong on the foreach-loop but I don't know, what it is or how to solve it.

My goal is to return the whole array like that:

//return
A B C D E F

CodePudding user response:

The issue you're having is that the foreach you wrote is trying to iterate over strings, while your actual contents in the list are string arrays.

Here's an example that compiles and runs on .NET Fiddle that shows how you could iterate over the structures you have declared. Also posting code here for posterity.

using System;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        
        List<string[,]> arrayList = new List<string[,]>();
        var twoDArray = new string[5,5];
        // just a couple of items for testing, intentionally leaving some indices null as well
        twoDArray[0,0] = "a";
        twoDArray[0,1] = "b";
        twoDArray[0,2] = "c";
        
        twoDArray[1,0] = "d";
        twoDArray[1,1] = "e";
        twoDArray[1,2] = "f";
        
        arrayList.Add(twoDArray);
        arrayList.Add(twoDArray);
        
        foreach(var array in arrayList)
        {
            Console.WriteLine("=== Starting iterration for another array in the array list ===");
            // iterates over the array as flattened items
            foreach(var arrayItem in array)
            {
                Console.WriteLine(string.Format("Flattened iteration - value {0}", arrayItem));
            }
            
            // or you can iterate over the items with their indices
            for (int x = 0; x < array.GetLength(0); x  = 1) 
            {
                for (int y = 0; y < array.GetLength(1); y  = 1) 
                {
                    var indexedItem = array[x,y];
                    Console.WriteLine(string.Format("Indexed iteration - at index {0},{1} value {2}", x, y, indexedItem));
                }
            }
        }
        
    }
}

CodePudding user response:

After doing some more trail-and-error-testing I've come up with this version:

    public override string ToString()
    {
        string test = "";

        foreach(string smth in listOfLists[0])
        {
            test  = smth;
        }

        return test;
    }

It magically works, what I find pretty interesting, because I've had some tries in which test = smth did not work for some reason and it would show me a strange error message in Unity. But I'll not question why it's magically working as wished.

Still thank you for answering the question.

  • Related