I want to do something like:
我想做的是:
object[] rowOfObjects = GetRow();//filled somewhere else
object[,] tableOfObjects = new object[10,10];
tableOfObjects[0] = rowOfObjects;
is this somehow possible and what is the syntax?
这可能吗?语法是什么?
or I need to do this:
或者我需要这样做:
for (int i = 0; i < rowOfObjects.Length; i++)
{
tableOfObjects[0,i] = rowOfObjects[i];
}
and fill up the 2 dimensional arrays row using a loop?
用循环填充二维数组行?
Thanks
谢谢
4 个解决方案
#1
7
No, if you are using a two dimensional array it's not possible. You have to copy each item.
不,如果你使用二维数组,这是不可能的。你必须复制每一项。
If you use a jagged array, it works just fine:
如果你使用的是锯齿状的数组,它的效果很好:
// create array of arrays
object[][] tableOfObject = new object[10][];
// create arrays to put in the array of arrays
for (int i = 0; i < 10; i++) tableOfObject[i] = new object[10];
// get row as array
object[] rowOfObject = GetRow();
// put array in array of arrays
tableOfObjects[0] = rowOfObjects;
If you are getting all the data as rows, you of course don't need the loop that puts arrays in the array of arrays, as you would just replace them anyway.
如果您将所有数据都作为行来获取,那么您当然不需要将数组放入数组数组的循环,因为您将只是替换它们。
#2
8
If your array is an array of value types, it is possible.
如果您的数组是一个值类型数组,那么它是可能的。
int[,] twoD = new int[2, 2] {
{0,1},
{2,3}
};
int[] oneD = new int[2]
{ 4, 5 };
int destRow = 1;
Buffer.BlockCopy(
oneD, // src
0, // srcOffset
twoD, // dst
destRow * twoD.GetLength(1) * sizeof(int), // dstOffset
oneD.Length * sizeof(int)); // count
// twoD now equals
// {0,1},
// {4,5}
It is not possible with an array of objects.
用对象数组是不可能的。
Note: Since .net3.5 this will only work with an array of primitives.
注意:因为。net3.5只能使用原语数组。
#3
1
if I have gigabyte size arrays, I would do it in C++/CLI playing with pointers and doing just memcpy instead of having gazillion slow boundary-checked array indexing operations.
如果我有gb大小的数组,我会用c++ /CLI操作指针,只做索引memcpy,而不是进行大量缓慢的边界检查数组操作。
#4
-1
So, Something like:
所以,类似:
public static object[] GetRow()
{
object[,] test = new object[10,10];
int a = 0;
object[] row = new object[10];
for(a = 0; a <= 10; a++)
{
row[a] = test[0, a];
}
return row;
}
#1
7
No, if you are using a two dimensional array it's not possible. You have to copy each item.
不,如果你使用二维数组,这是不可能的。你必须复制每一项。
If you use a jagged array, it works just fine:
如果你使用的是锯齿状的数组,它的效果很好:
// create array of arrays
object[][] tableOfObject = new object[10][];
// create arrays to put in the array of arrays
for (int i = 0; i < 10; i++) tableOfObject[i] = new object[10];
// get row as array
object[] rowOfObject = GetRow();
// put array in array of arrays
tableOfObjects[0] = rowOfObjects;
If you are getting all the data as rows, you of course don't need the loop that puts arrays in the array of arrays, as you would just replace them anyway.
如果您将所有数据都作为行来获取,那么您当然不需要将数组放入数组数组的循环,因为您将只是替换它们。
#2
8
If your array is an array of value types, it is possible.
如果您的数组是一个值类型数组,那么它是可能的。
int[,] twoD = new int[2, 2] {
{0,1},
{2,3}
};
int[] oneD = new int[2]
{ 4, 5 };
int destRow = 1;
Buffer.BlockCopy(
oneD, // src
0, // srcOffset
twoD, // dst
destRow * twoD.GetLength(1) * sizeof(int), // dstOffset
oneD.Length * sizeof(int)); // count
// twoD now equals
// {0,1},
// {4,5}
It is not possible with an array of objects.
用对象数组是不可能的。
Note: Since .net3.5 this will only work with an array of primitives.
注意:因为。net3.5只能使用原语数组。
#3
1
if I have gigabyte size arrays, I would do it in C++/CLI playing with pointers and doing just memcpy instead of having gazillion slow boundary-checked array indexing operations.
如果我有gb大小的数组,我会用c++ /CLI操作指针,只做索引memcpy,而不是进行大量缓慢的边界检查数组操作。
#4
-1
So, Something like:
所以,类似:
public static object[] GetRow()
{
object[,] test = new object[10,10];
int a = 0;
object[] row = new object[10];
for(a = 0; a <= 10; a++)
{
row[a] = test[0, a];
}
return row;
}