Display a multidimensional array

I have the following class

class Tile
{
    public int height;
    public int terrain;
}

And I have a 2D Tiles array

Tile[,] area = new Tile[5,5];

How do I display my area from Tile[,]to int[,]where only height is stored?

I tried to do this:

area.Select(tile => tile.height)

but, apparently, C # Multidimensional arrays do not implement IEnumerable.

How can I solve this problem?

+4
source share
4 answers

How can I solve this problem?

By writing the code. There is no "select" that works, so make your own:

static class Extensions 
{
  public static R[,] Select<T, R>(this T[,] items, Func<T, R> f) 
  {
    int d0 = items.GetLength(0);
    int d1 = items.GetLength(1);
    R[,] result = new R[d0, d1];
    for (int i0 = 0; i0 < d0; i0 += 1)
      for (int i1 = 0; i1 < d1; i1 += 1)
        result[i0, i1] = f(items[i0, i1]);
    return result;
  }

And now you have the extension method you want.

EXERCISES:

  • Which of the standard LINQ sequence operators makes sense to adapt to multidimensional arrays, and which ones don't?
  • , , LINQ, ?
+6

, reaaaallly, reaaaaallly want, - :

public static void Main(string[] args)
{
    var area = new Tile[5, 5];

    for (var j = 0; j < 5; j++)
        for (var i = 0; i < 5; i++)
            area[i, j] = new Tile() { height = (j + 1) * (i + 1), terrain = 99 };

linq:

    // this copies the data over from your area-array into a new int[5,5] array using
    // IEnumerable.Aggregate(...) with an emtpy seeded int[5,5] array and
    // leverages Enumerable.Range() with integer division + modular to get
    // the indices right

    var onlyHeights = Enumerable
        .Range(0, 25)
        .Aggregate(new int[5, 5], (acc, i) =>
    {
        acc[i / 5, i % 5] = area[i / 5, i % 5].height;
        return acc;
    });

:

    for (var j = 0; j < 5; j++)
        for (var i = 0; i < 5; i++)
            Console.WriteLine($"area.height {area[i, j].height} => {onlyHeights[i, j]}");
    Console.ReadLine();
}

:

area.height 1 => 1
area.height 2 => 2
area.height 3 => 3
area.height 4 => 4
area.height 5 => 5
area.height 2 => 2
area.height 4 => 4
area.height 6 => 6
area.height 8 => 8
area.height 10 => 10
area.height 3 => 3
area.height 6 => 6
area.height 9 => 9
area.height 12 => 12
area.height 15 => 15
area.height 4 => 4
area.height 8 => 8
area.height 12 => 12
area.height 16 => 16
area.height 20 => 20
area.height 5 => 5
area.height 10 => 10
area.height 15 => 15
area.height 20 => 20
area.height 25 => 25

for's .

+1

, , :

, :

public static class ArrayExtensions
{
    public static IEnumerable<T> ToEnumerable<T>(this Array target)
    {
    foreach (var item in target)
        yield return (T)item;
    }
}
+1

, , LINQ, .

public static class ArrayExtensions
{
    private static IEnumerable<int[]> CreatePermutations(int[] lengths, int pos = 0)
    {
        for (var i = 0; i < lengths[pos]; i++)
        {
            var newArray = (int[])lengths.Clone();
            newArray[pos] = i;
            if (pos + 1 >= lengths.Length)
            {
                yield return newArray;
                continue;
            }
            foreach (var next in CreatePermutations(newArray, pos + 1)) yield return next;
        }
    }
    public static Array Select<T,P>(this Array target, Func<T, P> func)
    {
        var dimensions = target.Rank;
        var lengths = Enumerable.Range(0, dimensions).Select(d => target.GetLength(d)).ToArray();
        var array = Array.CreateInstance(typeof(P), lengths);
        var permutations = CreatePermutations(lengths);
        foreach (var index in permutations)
        {
            array.SetValue(func((T)target.GetValue(index)), index);
        }
        return array;
    }
}

.

    var heightOnly = area.Select<Tile, int>(a => a.height);
+1

Source: https://habr.com/ru/post/1692278/


All Articles