简体   繁体   English

String.join在二维数组的一个维度上

[英]String.join on one dimension of a 2 dimensional array

I want to print a row of my 2-dimensional array with string.Join() and I couldn't find a way to do it efficiently. 我想用string.Join()打印一行我的二维数组,我找不到有效的方法。 Of course I could do it with a simple for loop but it would be interesting to know if there is a way to do it. 当然我可以通过一个简单的for循环来完成它,但知道是否有办法可以做到这一点很有意思。

EDIT I'm talking about a normal multidimensional array: 编辑我在谈论一个普通的多维数组:

int[,] multidimensionalArray= new int[,];

No way to isolate a row from a proper 2D array. 无法将行与正确的2D阵列隔离。 If it were a jagged array, then the solution would be obvious. 如果它是一个锯齿状阵列,那么解决方案就很明显了。

Therefore, if working with rows of the array is important operation to you, then you might consider turning it into the jagged array. 因此,如果使用数组的行对您来说是重要的操作,那么您可以考虑将其转换为锯齿状数组。

Otherwise, if this operation is not of fundamental importance, then a loop is the least disturbing way to go. 否则,如果这个操作不是很重要,那么循环是最不令人不安的方式。

You may choose to add a simple extension method for this purpose, and in that way put entire problem under the rug: 您可以选择为此目的添加一个简单的扩展方法,并以这种方式将整个问题置于地毯下:

public static class ArrayExtensions
{
    public static IEnumerable<T> GetRow<T>(this T[,] array, int rowIndex)
    {
        int columnsCount = array.GetLength(1);
        for (int colIndex = 0; colIndex < columnsCount; colIndex++)
            yield return array[rowIndex, colIndex];
    }
}

This would give you the option to address only one row: 这将为您提供仅处理一行的选项:

IEnumerable<int> row = array.GetRow(1);

For example, you could print one row from the matrix in one line of code: 例如,您可以在一行代码中从矩阵中打印一行:

Console.WriteLine(string.Join(", ", array.GetRow(1).ToArray()));

The answer of Zoran Horvat is good and that's what I would do if all I needed to do was read the array. Zoran Horvat的答案很好,如果我需要做的就是读数组,那就是我要做的。

If you also needed to write the array, you might do something like this: 如果您还需要编写数组,可以执行以下操作:

struct ArrayRow<T> // Consider implementing IEnumerable<T>
{
  T[,] array;
  int row;
  public ArrayRow(T[,] array, int row) 
  { 
    this.array = array; 
    this.row = row;
  }
  public T this[int i]
  {
    get { return this.array[this.row, i]; }
    set { this.array[this.row, i] = value; }
  }
  public int Length 
  {
    get { return this.array.GetLength(1); }
  }
  public IEnumerable<T> Items ()
  {
    int c = this.Length;
    for (int i = 0; i < c; ++i)
      yield return this[i];
  }
}

static class Extensions 
{
  public static ArrayRow<T> GetRow<T>(this T[,] array, int row)
  {
    return new ArrayRow<T>(array, row);
  }
}

And now you have something that looks like a one-dimensional array that actually writes to your two-dimensional array: 而现在你有一些看起来像一维数组的东西实际写入你的二维数组:

var myRow = myArray.GetRow(10);
myRow[20] += 30;
string.Join(",", myRow.Items());

And so on. 等等。

Don't know if you regard this more efficient or readable than a for loop, but you can do this: 不知道你是否认为这比for循环更有效或可读,但你可以这样做:

string s = string.Join(separator,
                       Enumerable.Range(0, multidimentionalarray.GetLength(1))
                                 .Select(column => multidimentionalarray[row, column]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM