【发布时间】:2016-05-25 18:24:52
【问题描述】:
我有一个使用多维数组数据结构的程序。数据被分配到多维数组中,一次一个数组(或行)(使用for 循环)。
例如,数组包含以下值:
double[][] values = new double[][]
{
{0.0, 0.1, 0.2, 0.3}, //each row added as 1
{1.0, 1.1, 1.2, 1.3},
{2.0, 2.1, 2.2, 2.3},
{3.0, 3.1, 3.2, 3.3}
};
在程序中的某些点,需要根据某些变量中保存的值返回多维数组的子集。但是,这需要将数组数据分组为列而不是行。
例如,像这样的:
//two dimensional array function to return subset of 'values'
public double[][] getArrayData(int startIndex, int endIndex)
{
//for sake of example, assume startIndex = 1, endIndex = 2
//returned structure would need to have the following values...
{0.1, 1.1, 2.1, 3.1}, //all values in 'row' with index 1
{0.2, 1.2, 2.2, 3.2} //all values in 'row' with index 2
//returns this 2D array
}
这实际上是将行数据转换为列数据,我预计这可以使用某种双 for 循环来完成。
有谁知道如何达到这个结果?
谢谢,
标记
【问题讨论】:
-
你能在.NETFiddle上添加一个样本吗?
-
dotnetfiddle.net/dbtgIm 我已经把它放在这里了,我只需要定义函数来完成预期的工作!谢谢@aloisdg
-
我开始认为我误解了您的要求。你想做什么?
-
您必须使用循环并通过手术获取列值。您拥有的结构可以轻松获取 row 的切片,但不能轻松获取 column。 C# 中没有内置的数据结构可以轻松让您获取行或列切片。
标签: c# arrays multidimensional-array