Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将新数组插入我的交错数组_C#_Jagged Arrays - Fatal编程技术网

C# 如何将新数组插入我的交错数组

C# 如何将新数组插入我的交错数组,c#,jagged-arrays,C#,Jagged Arrays,您好,我非常感谢您的帮助 好的,让我们看看,首先我声明了一个像这样的锯齿数组,然后是下一个代码 int n=1, m=3,p=0; int[][] jag_array =new[n]; 现在我的锯齿状数组将有1个数组在里面,接下来y必须像这样填充数组: car=2; do { jag_array[p]= new double[car]; for (int t = 0; t < carac; t++) { jag_a

您好,我非常感谢您的帮助

好的,让我们看看,首先我声明了一个像这样的锯齿数组,然后是下一个代码

int n=1, m=3,p=0;

int[][] jag_array =new[n];
现在我的锯齿状数组将有1个数组在里面,接下来y必须像这样填充数组:

car=2;
do
     {
     jag_array[p]= new double[car];
     for (int t = 0; t < carac; t++)
          {
           jag_array[p][t] = variableX;
          }
     p=p+1
     }
 while(p==0)
现在我的问题是,如果我声明

jag_array[p+1]= new double[car];
我将丢失previos one中的数据,我希望看起来像这样:

jag_array[0][0]=4
jag_array[0][1]=2
jag_array[1][0]=5
jag_array[1][1]=6

我没有从一开始就声明2数组的原因是因为我不知道要使用多少个数组,它可能只有1个或20个,每次我必须创建一个新的数组,而不丢失以前已填充的数据,请注意,

数组的大小,一旦创建,根据定义是不变的。如果您需要可变数量的元素,请使用-在您的示例中,可能是
列表

唯一的替代解决方案是创建一个具有新大小的新数组(并将其分配给
jag_array
变量),然后将以前的所有元素从旧数组复制到新数组中。当您只能使用
List
时,这是不必要的复杂代码,但如果出于任何原因无法使用
List
,下面是一个示例:

// increase the length of jag_array by one
var old_jag_array = jag_array; // store a reference to the smaller array
jag_array = new int[old_jag_array.Length + 1][]; // create the new, larger array
for (int i = 0; i < old_jag_array.Length; i++) {
    jag_array[i] = old_jag_array[i]; // copy the existing elements into the new array
}
jag_array[jag_array.Length - 1] = ... // insert new value here
//将jag_数组的长度增加1
var old_jag_数组=jag_数组;//存储对较小数组的引用
jag_数组=新整数[旧的jag_数组.Length+1][];//创建新的、更大的阵列
for(int i=0;i
任何不能使用
列表的原因
?@递归列表对于我的程序来说都不起作用reason@Daniel你这是什么意思?在C++中,数组是一种引用类型,所以数组的数组已经可以说是数组指针。是的,我认为这是一个C++问题。我也不建议用C++来做。这是真的吗?
int[][]
是c#中的“数组数组”而不是二维数组吗?丹尼尔:是的,这是真的。在C#中,二维数组将被写为
int[,]
,而
int[]][]
是数组数组。@JUAN:从您目前告诉我们的情况来看,还不清楚为什么
List
不起作用,所以显然没有足够的信息来说明您在寻找什么。请扩展您的问题以使其更清楚:-)
// increase the length of jag_array by one
var old_jag_array = jag_array; // store a reference to the smaller array
jag_array = new int[old_jag_array.Length + 1][]; // create the new, larger array
for (int i = 0; i < old_jag_array.Length; i++) {
    jag_array[i] = old_jag_array[i]; // copy the existing elements into the new array
}
jag_array[jag_array.Length - 1] = ... // insert new value here