Initialize a Jagged Array the LINQ Way

后端 未结 6 2145
孤城傲影
孤城傲影 2020-12-14 04:07

I have a 2-dimensional jagged array (though it\'s always rectangular), which I initialize using the traditional loop:

var myArr = new double[rowCount][];
for         


        
相关标签:
6条回答
  • 2020-12-14 04:37

    What you have won't work as the new occurs before the call to Repeat. You need something that also repeats the creation of the array. This can be achieved using the Enumerable.Range method to generate a range and then performing a Select operation that maps each element of the range to a new array instance (as in Amy B's answer).

    However, I think that you are trying to use LINQ where it isn't really appropriate to do so in this case. What you had prior to the LINQ solution is just fine. Of course, if you wanted a LINQ-style approach similar to Enumerable.Repeat, you could write your own extension method that generates a new item, such as:

        public static IEnumerable<TResult> Repeat<TResult>(
              Func<TResult> generator,
              int count)
        {
            for (int i = 0; i < count; i++)
            {
                yield return generator();
            }
        }
    

    Then you can call it as follows:

       var result = Repeat(()=>new double[rowCount], columnCount).ToArray();
    
    0 讨论(0)
  • 2020-12-14 04:38

    I just wrote this function...

        public static T[][] GetMatrix<T>(int m, int n)
        {
            var v = new T[m][];
            for(int i=0;i<m; ++i) v[i] = new T[n];
            return v;
        }
    

    Seems to work.

    Usage:

    float[][] vertices = GetMatrix<float>(8, 3);
    
    0 讨论(0)
  • 2020-12-14 04:43

    You can't do that with the Repeat method : the element parameter is only evaluated once, so indeed it always repeats the same instance. Instead, you could create a method to do what you want, which would take a lambda instead of a value :

        public static IEnumerable<T> Sequence<T>(Func<T> generator, int count)
        {
            for (int i = 0; i < count; i++)
            {
                yield return generator();
            }
        }
    
        ...
    
        var myArr = Sequence(() => new double[colCount], rowCount).ToArray();
    
    0 讨论(0)
  • 2020-12-14 04:45

    What about

    var myArr = new double[rowCount, colCount];
    

    or

    double myArr = new double[rowCount, colCount];
    

    Reference: http://msdn.microsoft.com/en-us/library/aa691346(v=vs.71).aspx

    0 讨论(0)
  • 2020-12-14 04:47
    double[][] myArr = Enumerable
      .Range(0, rowCount)
      .Select(i => new double[colCount])
      .ToArray();
    
    0 讨论(0)
  • 2020-12-14 04:49

    The behavior is correct - Repeat() returns a sequence that contains the supplied object multiple times. You can do the following trick.

    double[][] myArr = Enumerable
        .Repeat(0, rowCount)
        .Select(i => new double[colCount])
        .ToArray();
    
    0 讨论(0)
提交回复
热议问题