我试图向char数组中添加一个字符,但是在这样做时我会收到一个NullReferenceException。我的代码如下:
string[] triples = new string[10];
for(int i = 0; i < triples.Length; i += 3) {
triples[i] = bits.Substring(i, 3);
}
char[] corrected = new char[triples.Length];
for(int i = 0; i < triples.Length; i++) {
int zero = 0;
int one = 0;
foreach(char j in triples[i]) {
if(j == '0') {
zero += 1;
} else {
one += 1;
}
}
if(zero > one) {
corrected[i] = '0';
} else {
corrected[i] = '1';
}
}这个问题出现在corrected[i] = '0';行上。
bits是String
我已经看过this问题,特别是关于数组的部分,但是我找不到解决方案。
发布于 2021-06-29 17:29:35
这里是你的代码分析。
// Set triples[i] for i = 0, 3, 6, 9 ...
for(int i = 0; i < triples.Length; i += 3) {
triples[i] = bits.Substring(i, 3);
}
for(int i = 0; i < triples.Length; i++) {
// Access triples[i] for i = 0, 1, 2, 3, 4 ...
// So for i = 1, triples[i] is null and you get an exception
foreach(char j in triples[i]) {
}
}https://stackoverflow.com/questions/68183137
复制相似问题