14

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

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

从中提取一维数组(单行或单列)的最佳方法是什么?当然,我可以使用 for 循环来做到这一点,但我希望 .NET 有一种更优雅的内置方式。

将提取的字符串数组转换为对象数组的奖励积分。

4

6 回答 6

13

您可以轻松地将字符串数组转换为对象数组 - 反之亦然。但是,据我所知,实际提取必须Array.Copy使用 for 循环:要求源和目标等级相同,并且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方法并重用它们。

于 2008-10-24T05:30:32.803 回答
8

对于矩形阵列:

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();
于 2008-10-24T13:15:41.557 回答
4

我只是想澄清一下(给出例子和被问到的内容)。

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

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;

希望这可以帮助 :)

于 2008-10-24T06:42:51.267 回答
1

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++;
    }
}
于 2008-10-24T05:24:53.483 回答
1

可以使用 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);

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

于 2008-10-24T09:04:25.133 回答
0

我做了扩展方法。我不知道性能。

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) ; 
于 2015-12-30T08:27:02.730 回答