C# 如何将一维数组转换为多维数组?(C#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1182983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert single dimensional arrays to multi dimensional ones? (C#)
提问by Jader Dias
I have to use a method which accepts double[,], but I only have a double[]. How can I convert it?
我必须使用一种接受double[,] 的方法,但我只有一个double[]。我怎样才能转换它?
Solution so far:
到目前为止的解决方案:
var array = new double[1, x.Length];
foreach (var i in Enumerable.Range(0, x.Length))
{
array[0, i] = x;
}
采纳答案by Jader Dias
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;
}
}
}
回答by Mehrdad Afshari
There's no direct way. You should copy stuff into a double[,]
. Assuming you want it in a single row:
没有直接的办法。您应该将内容复制到double[,]
. 假设您希望将其放在一行中:
double[,] arr = new double[1, original.Length];
for (int i = 0; i < original.Length; ++i)
arr[0, i] = original[i];
回答by Jared Updike
Mehrdad assumes that the width is one since there is no real way to determine either the width or height from a one dimensional array by itself. If you have some (outside) notion of the 'width' then Mehrdad's code becomes:
Mehrdad 假设宽度为 1,因为没有真正的方法可以从一维数组本身确定宽度或高度。如果您对“宽度”有一些(外部)概念,则 Mehrdad 的代码变为:
// assuming you have a variable with the 'width', pulled out of a rabbit's hat
int height = original.Length / width;
double[,] arr = new double[width, height];
int x = 0;
int y = 0;
for (int i = 0; i < original.Length; ++i)
{
arr[x, y] = original[i];
x++;
if (x == width)
{
x = 0;
y++;
}
}
Although, Row majoris probably more common in many applications (matrices, text buffers or graphics):
虽然,Row major在许多应用程序(矩阵、文本缓冲区或图形)中可能更常见:
// assuming you have a variable with the 'width', pulled out of a rabbit's hat
int height = original.Length / width;
double[,] arr = new double[height, width]; // note the swap
int x = 0;
int y = 0;
for (int i = 0; i < original.Length; ++i)
{
arr[y, x] = original[i]; // note the swap
x++;
if (x == width)
{
x = 0;
y++;
}
}
回答by ShawnFeatherly
If you know the width of the 2D array you can use the following to put the values in as one row after another.
如果您知道 2D 数组的宽度,则可以使用以下内容将值作为一行接一行地放入。
private T[,] toRectangular<T>(T[] flatArray, int width)
{
int height = (int)Math.Ceiling(flatArray.Length / (double)width);
T[,] result = new T[height, width];
int rowIndex, colIndex;
for (int index = 0; index < flatArray.Length; index++)
{
rowIndex = index / width;
colIndex = index % width;
result[rowIndex, colIndex] = flatArray[index];
}
return result;
}