简体   繁体   English

确定在C#中的数组的大小?

[英]Determining size of an array of arrays in C#?

I am creating an array of arrays such that: 我正在创建这样的数组数组:

var arrNewArray = new string[arrOldArray.Length][7];

arrOldArray is an array of arrays such that it's [X][4], meaning the length of the 1st array or "outside" array can change, but the length of the "inside" array is ALWAYS 4, or hold 4 strings ([0][1][2][3]). arrOldArray是一个数组数组,其值为[X] [4],这意味着第一个数组或“外部”数组的长度可以更改,但是“内部”数组的长度始终为4,或保留4个字符串([ 0] [1] [2] [3])。

Why won't the compiler accept my statement above? 编译器为什么不接受我的上述声明?

Essentially, I'm trying to take arrOldArray and expand it, or add a few more "columns" by increasing the [4] in the old array to a [7] in the new array and then copy the contents over. 本质上,我试图采用arrOldArray并将其扩展,或者通过将旧数组中的[4]增加到新数组中的[7],然后添加更多“列”,然后将内容复制过来。 Perhaps I'm not doing it the best/efficient way, so any guidance would be appreciated thanks. 也许我没有以最佳/有效的方式来做,所以任何指导都将不胜感激。

I think you want a two dimensional array: 我想你想要一个二维数组:

var arrNewArray = new string[arrOldArray.Length, 7];

You would access it like this: arrNewArray[x, y] . 您可以这样访问它: arrNewArray[x, y]

This is better than a jagged array, because it clearly communicates that the number of "columns" is the same for every row. 这比锯齿数组更好,因为它清楚地表明每行的“列”数是相同的。

If you want to continue using a jagged array, you need to do it like this: 如果要继续使用锯齿状数组,则需要这样做:

var arrNewArray = new string[arrOldArray.Length][];
for(int i = 0; i < arrOldArray.Length; ++i)
    arrNewArray[i] = new string[7];

The reason for this convoluted way is: With a jagged array, each "row" can have a different number of "columns". 这种令人费解的方式的原因是:对于锯齿状数组,每个“行”可以具有不同数量的“列”。 A short-hand syntax for the case where each "row" has the same number of "columns" doesn't exist. 对于每个“行”具有相同数量的“列”的情况,不存在简写语法。 That's why your code doesn't compile. 这就是为什么您的代码无法编译的原因。
A jagged array is essential an array of arrays, so you need to create a new array instance for each "row" of the outer array and explicitly assign it. 锯齿状的数组对于数组的数组至关重要,因此您需要为外部数组的每个“行”创建一个新的数组实例,并对其进行显式分配。 That's what the for loop is doing. 这就是for循环正在做的事情。

You can't use Array.Copy with jagged arrays. 您不能将Array.Copy与锯齿状数组一起使用。 Each child-array is it's own instance and Array.Copy doesn't make a deep copy, it merely copies the references from one array to another. 每个子数组都是它自己的实例,而Array.Copy并不进行深拷贝,它只是将引用从一个数组复制到另一个数组。 The effect would be, that both arrays would point to the same items and changing an item in one array would be seen from the other. 效果是,两个数组都指向相同的项目,而从另一个数组中看到一个项目中的更改项目。

You are not creating the jagged array properly. 您没有正确创建锯齿状数组。 The proper way is to create the first dimension of the jagged array and then loop through the items of the first dimension to create the nested arrays and copy the data from the old arrays. 正确的方法是创建锯齿状数组的第一维,然后循环遍历第一维的各项以创建嵌套数组并从旧数组中复制数据。 Here's an example: 这是一个例子:

int newSize = 7;
string[][] newArray = new string[oldArray.Length][];
for (int i = 0; i < oldArray.Length; i++)
{
    newArray[i] = new string[newSize];
    Array.Copy(oldArray[i], newArray[i], oldArray[i].Length);
}

你会想要

var arrNewArray = new string[arrOldArray.Length, 7];
var arrNewArray = new[] {new string[7]};//array of arrays
var arrNewArray = new string[arrOldArray.Length, 7];//two-dimensional array

Using Linq: 使用Linq:

int[][] jaggedArray2 = new int[][] 
{
    new int[] {1,3,5,7,9},
    new int[] {0,2,4,6},
    new int[] {11,22}
};
int length = jaggedArray.Sum(a => a.Length);

I don't believe what you're asking is directly possible. 我不认为您的要求是直接可能的。 Because the syntax that you are using is for a jagged array, and what you are doing is effectively asking it to create a multi-dimensional array. 因为您使用的语法是针对锯齿状数组的,所以您实际上在要求它创建多维数组。

The syntax is confusing since it reads like what you really want is a multi-dimensional array (although I'm aware that's not the case.) 语法令人困惑,因为它看起来像您真正想要的是多维数组(尽管我知道不是这种情况。)

I don't believe you could store your arrays in the newly allocated array either due to a size change. 我不相信由于大小的变化,您也不能将数组存储在新分配的数组中。 You would need to build a custom copy method to move the data into the larger array. 您将需要构建一个自定义复制方法,以将数据移入更大的数组。

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

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