Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# 4.0 C#用于循环增量为2的故障_C# 4.0 - Fatal编程技术网

C# 4.0 C#用于循环增量为2的故障

C# 4.0 C#用于循环增量为2的故障,c#-4.0,C# 4.0,该算法通过将“A”、“B”存储到索引8和索引9,将字符串从数组A存储到数组B 我真的开始将B的数组大小设置为10,因为稍后我会在那里放置一些其他东西 我的部分代码: string[] A = new string[]{"A","B"} string[] B = new string[10]; int count; for(count = 0; count < A.length; count++) { B[count] = A[count] } string[]A=新字符串

该算法通过将“A”、“B”存储到索引8和索引9,将字符串从数组A存储到数组B 我真的开始将B的数组大小设置为10,因为稍后我会在那里放置一些其他东西

我的部分代码:

string[] A = new string[]{"A","B"}
string[] B = new string[10]; 
int count;

for(count = 0; count < A.length; count++)
{
      B[count] = A[count]
}
string[]A=新字符串[]{“A”,“B”}
字符串[]B=新字符串[10];
整数计数;
用于(计数=0;计数
因此,您希望用2增加每个索引:

string[] A = new string[] { "A", "B", "C", "D" };
string[] B = new string[A.Length + 2];
for (int i = 0; i < A.Length; i++)
{
    B[i + 2] = A[i];
}
编辑:那么您希望从B中的索引0开始,并始终留有间隙

string[] A = new string[] { "A", "B", "C", "D" };
string[] B = new string[A.Length * 2 + 2]; // you wanted to add something other as well
for (int i = 0; i/2 < A.Length; i+=2)
{
    B[i] = A[i / 2];
}
更新“除此之外还有其他编码吗?”

您可以使用Linq,尽管它的可读性和效率不如简单的循环:

String[] Bs = Enumerable.Range(0, A.Length * 2 + 2) // since you want two empty places at the end
 .Select((s, i) => i % 2 == 0 && i / 2 < A.Length ? A[i / 2] : null)
 .ToArray();

再次尝试猜测你想要什么:

string[] A = new string[] { "A", "B", "C", "D" };
string[] B = new string[A.Length * 2];
for (int i = 0; i < A.Length; i++)
{
    B[i*2] = A[i];
}
string[]A=新字符串[]{“A”、“B”、“C”、“D”};
字符串[]B=新字符串[A.长度*2];
for(int i=0;i
想象一下如何将某个值增加2?你知道
count++
是什么意思吗?
*
在C#中做乘法。
count+=2
在C#中for循环的更新端需要使用复合赋值。@chrisaplon:我已经编辑了我的答案。你的问题中也应该包括所需的输出。是的,先生,除此之外还有其他编码吗?对于像我这样的初学者来说是很难理解的。@ChrisAplaon:为了完整起见,添加了(不合适的)Linq方法。哇,我在网站的其他问题中看到了这种编码,但我仍然很难理解。顺便说一下,先生,非常感谢。即使我手动跟踪代码也能正常工作。先生,我想补充一个问题。可以
String[] Bs = Enumerable.Range(0, A.Length * 2 + 2) // since you want two empty places at the end
 .Select((s, i) => i % 2 == 0 && i / 2 < A.Length ? A[i / 2] : null)
 .ToArray();
for (int i = 1; (i-1) / 2 < A.Length; i += 2)
{
    B[i] = A[(i-1) / 2];
}
Index: 0 Value: 
Index: 1 Value: A
Index: 2 Value: 
Index: 3 Value: B
Index: 4 Value: 
Index: 5 Value: C
Index: 6 Value: 
Index: 7 Value: D
Index: 8 Value: 
Index: 9 Value
string[] A = new string[] { "A", "B", "C", "D" };
string[] B = new string[A.Length * 2];
for (int i = 0; i < A.Length; i++)
{
    B[i*2] = A[i];
}