在处理(基于java的语言)中我可以做到这一点;
int [][] a = new int[3][2];
for (int i = 0; i < 3; i++)
{
a[i][0] = i;
a[i][1] = i+3;
}
int [] b = a[2];
b 是一个一维数组,值为 2 和 5。它采用数组 a 第 2 行中的数组。
matlab 语法为
b = a(3,:);
c# 有等效的吗?我似乎找不到任何东西,但似乎是一个相当有用且明显的功能。
谢谢
最佳答案
C# 有 multidimensional arrays ,但它也有数组的数组(也称为 jagged arrays )。第二种形式就是您所追求的:
// This is a jagged array. It has 3 rows, each of which is an int[] in its own right.
// Each row can also have a different number of elements from all the others, so if
// a[i][N] is valid for some i and N, a[x][N] is not necessarily valid for other x != i
var a = new int[3][];
// To populate the array you need to explicitly create an instance for each sub-array
for (int i = 0; i < 3; i++)
{
a[i] = new[] { i, i + 3 };
}
// And now this is possible:
var b = a[2];
如果您手头有一个多维数组,则需要手动进行复制。
关于c# - 复制多维数组的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11242092/