如何转换一维数组,以多维的? (C#) [英] How to convert single dimensional arrays to multi dimensional ones? (C#)

查看:207
本文介绍了如何转换一维数组,以多维的? (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不使用它接受双重的方法[,],但我只有一个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;
        }
    }
}

这篇关于如何转换一维数组,以多维的? (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆