即時嘗試使用隨機數來從30個字符串中取出58個字符串的數組,並且使用布爾數組來檢查並確保相同的數字不會被調用兩次。該方法和程序總是崩潰,索引超出範圍錯誤。這裏是方法。在布爾數組(c#)中超出範圍異常
static string[] newlist(string[] s)
{
string[] newlist = {};
bool[] issearched = new bool[s.Length];
Random callorder = new Random();
for (int i = 0; i < 31; i++)
{
int number = callorder.Next(0, s.Length);
if (issearched[number] == false)
{
newlist[number] = s[number];
issearched[number] = true;//this is where it always crashes even though the ide says issearced has 58 elements and the random number is always smaller than that.
}
else
i--;
}
return newlist;
}
我確定它的簡單,但我想不出爲什麼8指數是58
的值沒有你的陣列newlist不是58元件長度。 – Steve 2014-10-26 23:57:30
它的[s.length]的長度是58.視覺工作室說58當我在折點處滾動它時。 – vexedpython 2014-10-27 00:01:48
再次檢查issearched的長度不是新列表的長度,您正在分配給newlist而不是issearched,例外情況是在newlist assignement分配 – Steve 2014-10-27 00:09:40