假设我有一个矩形字符串数组 - 不是锯齿状数组

string[,] strings = new string[8, 3];

从中提取一维数组(单行还是单列)的最佳方法是什么?当然,我可以使用for循环执行此操作,但我希望.NET内置更优雅的方式。

将提取的字符串数组转换为对象数组的加值点。

有帮助吗?

解决方案

对于矩形阵列:

string[,] rectArray = new string[3,3] { 
    {"a", "b", "c"}, 
    {"d", "e", "f"}, 
    {"g", "h", "i"} };

var rectResult = rectArray.Cast<object>().ToArray();

对于锯齿状阵列:

string[][] jaggedArray =  { 
    new string[] {"a", "b", "c", "d"}, 
    new string[] {"e", "f"}, 
    new string[] {"g", "h", "i"} };

var jaggedResult = jaggedArray.SelectMany(s => s).Cast<object>().ToArray();

其他提示

您可以轻松地将字符串数组转换为对象数组 - 另一种方式不起作用。实际提取使用for循环,据我所知: Array.Copy 要求源和目标排名相同,并且 Buffer.BlockCopy 仅适用于值类型数组。虽然看起来很奇怪......

你可以使用LINQ在单个语句中添加一行或一列,虽然效率很低(因为它会在内部构建一个列表,然后必须将其转换为一个数组 - 如果你自己做,你可以将数组预先分配到正确的大小并直接复制。

复制一行( rowNum 是要复制的行):

object[] row = Enumerable.Range(0, rowLength)
                         .Select(colNum => (object) stringArray[rowNum, colNum])
                         .ToArray();

复制列( colNum 是要复制的列):

object[] column = Enumerable.Range(0, columnLength)
                            .Select(rowNum => (object) stringArray[rowNum, colNum])
                            .ToArray();

我不确定这是否比foreach循环更好/更简单 - 特别是如果你编写 ExtractRow 方法和 ExtractColumn 方法并重用它们

我想澄清一下(给出一个例子以及被问到的内容)。

锯齿状数组是一个数组数组,声明如下:

string[][] data = new string[3][];
data[0] = new string[] { "0,[0]", "0,[1]", "0,[2]" };
data[1] = new string[] { "1,[0]", "1,[1]", "1,[2]" ];
data[2] = new string[] { "2,[0]", "1,[1]", "1,[2]" };

矩形数组定义为包含多个维度的单个数组:

string[,] data = new string[3,3];
data[0,0] = "0,0";
data[0,1] = "0,1";
data[0,2] = "0,2";
...etc

因此, 锯齿状数组 是IQueryable / IEnumerable,因为您可以迭代它以在每次迭代时接收数组。而 矩形数组 IQueryable / IEnumerable,因为元素以完整维度(0,0 0,1..etc)进行处理,因此您在这种情况下,将无法使用Linq或为Array创建的任何预定义函数。

虽然您可以迭代一次(并实现您想要的)数组:

/// INPUT: rowIndex, OUTPUT: An object[] of data for that row
int colLength = stringArray.GetLength(1);
object[] rowData = new object[colLength];
for (int col = 0; col < colLength; col++) {
    rowData[col] = stringArray[rowIndex, col] as object;
}
return rowData;

/// INPUT: colIndex, OUTPUT: An object[] of data for that column
int rowLength = stringArray.GetLength(0);
object[] colData = new object[rowLength];
for (int row = 0; r < rowLength; row++) {
    colData[row] = stringArray[row, colIndex] as object;
}
return colData;

希望这会有所帮助:)

LINQ就是答案

static object[] GetColumn(string[][] source, int col) {
    return source.Iterate().Select(x => source[x.Index][col]).Cast<object>().ToArray();
}
static object[] GetRow(string[][] source, int row) {
    return source.Skip(row).First().Cast<object>().ToArray();
}
public class Pair<T> {
    public int Index;
    public T Value;
    public Pair(int i, T v) {
        Index = i;
        Value = v;
    }
}
static IEnumerable<Pair<T>> Iterate<T>(this IEnumerable<T> source) {
    int index = 0;
    foreach (var cur in source) {
        yield return new Pair<T>(index, cur);
        index++;
    }
}

可以使用Array.Copy轻松复制行:

        int[][] arDouble = new int[2][];
        arDouble[0] = new int[2];
        arDouble[1] = new int[2];
        arDouble[0][0] = 1;
        arDouble[0][1] = 2;
        arDouble[1][0] = 3;
        arDouble[1][1] = 4;

        int[] arSingle = new int[arDouble[0].Length];

        Array.Copy(arDouble[0], arSingle, arDouble[0].Length);

这会将第一行复制到单个Dimension数组中。

我做了扩展方法。我不知道表现。

public static class ExtensionMethods 
{
     public static string[] get1Dim(this string[,] RectArr, int _1DimIndex , int _2DimIndex   )
     {
        string[] temp = new string[RectArr.GetLength(1)];

        if (_2DimIndex == -1)
        {
          for (int i = 0; i < RectArr.GetLength(1); i++)
          {   temp[i] = RectArr[_1DimIndex, i];    }
        }
        else
        {
          for (int i = 0; i < RectArr.GetLength(0); i++)
          {   temp[i] = RectArr[  i , _2DimIndex];    }
        }

         return temp;
      }
}

用法

// we now have this funtionaliy RectArray[1, * ]  
//                                       -1 means ALL    
string[] _1stRow = RectArray.get1Dim( 0, -1) ;    
string[] _2ndRow = RectArray.get1Dim( 1, -1) ; 

string[] _1stCol = RectArray.get1Dim( -1, 0) ;    
string[] _2ndCol = RectArray.get1Dim( -1, 1) ; 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top