I'm trying to figure out how to get a single dimension from a multidimensional array (for the sake of argument, let's say it's 2D), I have a multidimensional array:
我想知道如何从多维数组中得到一个维度(为了讨论,假设它是2D的),我有一个多维数组:
double[,] d = new double[,] { { 1, 2, 3, 4, 5 }, { 5, 4, 3, 2, 1 } };
If it was a jagged array, I would simply call d[0]
and that would give me an array of {1, 2, 3, 4, 5}
, is there a way I can achieve the same with a 2D array?
如果它是一个不规则的数组,我只需调用d[0],它就会给我一个{1,2,3,4,5}的数组,有什么方法可以用2D数组来实现同样的效果吗?
3 个解决方案
#1
17
No. You could of course write a wrapper class that represents a slice, and has an indexer internally - but nothing inbuilt. The other approach would be to write a method that makes a copy of a slice and hands back a vector - it depends whether you want a copy or not.
不。当然,您可以编写一个表示切片的包装器类,并在内部拥有索引器——但内部没有索引。另一种方法是编写一个方法来复制一个切片并返回一个向量——这取决于您是否需要复制。
using System;
static class ArraySliceExt
{
public static ArraySlice2D<T> Slice<T>(this T[,] arr, int firstDimension)
{
return new ArraySlice2D<T>(arr, firstDimension);
}
}
class ArraySlice2D<T>
{
private readonly T[,] arr;
private readonly int firstDimension;
private readonly int length;
public int Length { get { return length; } }
public ArraySlice2D(T[,] arr, int firstDimension)
{
this.arr = arr;
this.firstDimension = firstDimension;
this.length = arr.GetUpperBound(1) + 1;
}
public T this[int index]
{
get { return arr[firstDimension, index]; }
set { arr[firstDimension, index] = value; }
}
}
public static class Program
{
static void Main()
{
double[,] d = new double[,] { { 1, 2, 3, 4, 5 }, { 5, 4, 3, 2, 1 } };
var slice = d.Slice(0);
for (int i = 0; i < slice.Length; i++)
{
Console.WriteLine(slice[i]);
}
}
}
#2
5
Improved version of that answer:
这个答案的改进版本:
public static IEnumerable<T> SliceRow<T>(this T[,] array, int row)
{
for (var i = array.GetLowerBound(1); i <= array.GetUpperBound(1); i++)
{
yield return array[row, i];
}
}
public static IEnumerable<T> SliceColumn<T>(this T[,] array, int column)
{
for (var i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
{
yield return array[i, column];
}
}
#3
3
Rectangular arrays are not built for this purpose. If you need that type of functionality, you should switch to a jagged array. It is pretty simple to write a function that will convert a rectangular array into a jagged one.
矩形数组不是为此目的而构建的。如果您需要这种类型的功能,您应该切换到一个交错数组。编写一个将矩形数组转换成锯齿状数组的函数非常简单。
You could also simply rebuild that array by calling GetLength(int dimension) on the appropriate dimension, and then indexing it properly to retrieve each value. It would be cheaper than converting the entire array, but the cheapest option is to change it to use jagged arrays.
您还可以通过在适当的维度上调用GetLength(int维度)来重新构建该数组,然后对其进行适当的索引以检索每个值。它比转换整个数组要便宜,但是最便宜的选择是使用交错数组。
#1
17
No. You could of course write a wrapper class that represents a slice, and has an indexer internally - but nothing inbuilt. The other approach would be to write a method that makes a copy of a slice and hands back a vector - it depends whether you want a copy or not.
不。当然,您可以编写一个表示切片的包装器类,并在内部拥有索引器——但内部没有索引。另一种方法是编写一个方法来复制一个切片并返回一个向量——这取决于您是否需要复制。
using System;
static class ArraySliceExt
{
public static ArraySlice2D<T> Slice<T>(this T[,] arr, int firstDimension)
{
return new ArraySlice2D<T>(arr, firstDimension);
}
}
class ArraySlice2D<T>
{
private readonly T[,] arr;
private readonly int firstDimension;
private readonly int length;
public int Length { get { return length; } }
public ArraySlice2D(T[,] arr, int firstDimension)
{
this.arr = arr;
this.firstDimension = firstDimension;
this.length = arr.GetUpperBound(1) + 1;
}
public T this[int index]
{
get { return arr[firstDimension, index]; }
set { arr[firstDimension, index] = value; }
}
}
public static class Program
{
static void Main()
{
double[,] d = new double[,] { { 1, 2, 3, 4, 5 }, { 5, 4, 3, 2, 1 } };
var slice = d.Slice(0);
for (int i = 0; i < slice.Length; i++)
{
Console.WriteLine(slice[i]);
}
}
}
#2
5
Improved version of that answer:
这个答案的改进版本:
public static IEnumerable<T> SliceRow<T>(this T[,] array, int row)
{
for (var i = array.GetLowerBound(1); i <= array.GetUpperBound(1); i++)
{
yield return array[row, i];
}
}
public static IEnumerable<T> SliceColumn<T>(this T[,] array, int column)
{
for (var i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
{
yield return array[i, column];
}
}
#3
3
Rectangular arrays are not built for this purpose. If you need that type of functionality, you should switch to a jagged array. It is pretty simple to write a function that will convert a rectangular array into a jagged one.
矩形数组不是为此目的而构建的。如果您需要这种类型的功能,您应该切换到一个交错数组。编写一个将矩形数组转换成锯齿状数组的函数非常简单。
You could also simply rebuild that array by calling GetLength(int dimension) on the appropriate dimension, and then indexing it properly to retrieve each value. It would be cheaper than converting the entire array, but the cheapest option is to change it to use jagged arrays.
您还可以通过在适当的维度上调用GetLength(int维度)来重新构建该数组,然后对其进行适当的索引以检索每个值。它比转换整个数组要便宜,但是最便宜的选择是使用交错数组。