【发布时间】:2012-11-11 02:40:22
【问题描述】:
我试图编写一个随机字符串数组算法,但我得到一个空引用错误..我不知道为什么..
public static string[] arrRandomized;
public static string[] ShuffleWords(string[] Words)
{
Random generator = new Random();
for (int i=0;i < Words.Length; i++) {
int pos = generator.Next(Words.Length);
Console.WriteLine(Words[pos]); // I SEE RANDOM ITEM
Console.Read(); // NULL REFERENCE ERROR AFTER THIS
if (Words[pos] != null)
{
arrRandomized[i] = Words[pos];
//remove item at pos so I get no duplicates
Words[pos] = null;
}
}
我不想使用 ArrayList,我有我的理由,但那是题外话,我只是想知道这怎么不起作用:/ 谢谢
【问题讨论】:
-
你在哪里使用 Console.Read() ?
-
不建议在迭代时更改集合(或数组)。
-
“我不知道为什么..” 调试它。
-
还要特别注意
arrRandomized。