如何转换一维数组,以多维的? (C#)多维、数组

2023-09-05 02:43:23 作者:流年 "

我不得不使用它接受双重的方法[,],但我只有一个double []。我如何转换呢?

解决方案至今:

  VAR阵列=新的双[1,x.Length]
的foreach(在Enumerable.Range变种I(0,x.Length))
{
    阵列[0,1] = X;
}
 

解决方案

我刚刚写了这个code,我将使用:

 使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq的;

命名空间MiscellaneousUtilities
{
    公共静态类可枚举
    {
        公共静态T [,] ToRow< T>(这IEnumerable的< T>目标)
        {
            变种阵列= target.ToArray();
            无功输出=新T [1,array.Length]
            的foreach(在System.Linq.Enumerable.Range变种I(0,array.Length))
            {
                输出[0,1] =阵列[I]
            }
            返回输出;
        }

        公共静态T [,] ToColumn< T>(这IEnumerable的< T>目标)
        {
            变种阵列= target.ToArray();
            无功输出=新T [array.Length,1];
            的foreach(在System.Linq.Enumerable.Range变种I(0,array.Length))
            {
                输出[I,0] =阵列[I]
            }
            返回输出;
        }
    }
}
 

如何能让一维数组转换成二维数组

I have to use a method which accepts double[,], but I only have a double[]. How can I convert it?

Solution so far:

var array = new double[1, x.Length];
foreach (var i in Enumerable.Range(0, x.Length))
{
    array[0, i] = x;
}

解决方案

I just wrote this code which I will use:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace MiscellaneousUtilities
{
    public static class Enumerable
    {
        public static T[,] ToRow<T>(this IEnumerable<T> target)
        {
            var array = target.ToArray();
            var output = new T[1, array.Length];
            foreach (var i in System.Linq.Enumerable.Range(0, array.Length))
            {
                output[0, i] = array[i];
            }
            return output;
        }

        public static T[,] ToColumn<T>(this IEnumerable<T> target)
        {
            var array = target.ToArray();
            var output = new T[array.Length, 1];
            foreach (var i in System.Linq.Enumerable.Range(0, array.Length))
            {
                output[i, 0] = array[i];
            }
            return output;
        }
    }
}