iT邦幫忙

0

C# 索引超出範圍 必須為非負數且小於集合的大小 參數名稱 index

c#
List<String>A_Word=newList<String>();//字(A)
List<Double>A_Term=newList<Double>();//特徵值(A)
Int32A_Word_num=0;//字數(A)
List<String>B_Word=newList<String>();//字(B)
List<Double>B_Term=newList<Double>();//特徵值(B)
Int32B_Word_num=0;//字數(B)
//DoublexSquare_critical;
//DoublexSquare_statistics;
Int32[]_Flag=newInt32[]{0,0};//_Flag[0].._Testing,_Flag[1].._Training



while(_Flag[0]!=_Testing._Term.Count&&_Flag[1]!=this._Training[a]._Term.Count)
{
Int32_Compare=String.Compare(_Testing._Term[_Flag[0]],this._Training[a]._Term[_Flag[1]]);
if(_Compare<0)
{
B_Word.Add(_Testing._Term[_Flag[0]]);
B_Term.Add(_Testing._Value[_Flag[0]]);
_Flag[0]+=1;
}
elseif(_Compare>0)
{
A_Word.Add(this._Training[a]._Term[_Flag[1]]);
A_Term.Add(this._Training[a]._Value[_Flag[1]]);
_Flag[1]+=1;
}
else
{
A_Word.Add(this._Training[a]._Term[_Flag[1]]);
B_Word.Add(_Testing._Term[_Flag[0]]);
A_Term.Add(this._Training[a]._Value[_Flag[1]]);
B_Term.Add(_Testing._Value[_Flag[0]]);
_Flag[0]+=1;
_Flag[1]+=1;
}
}
while(_Flag[0]!=_Testing._Term.Count)
{
B_Word.Add(_Testing._Term[_Flag[0]]);
B_Term.Add(_Testing._Value[_Flag[0]]);
_Flag[0]+=1;
}
while(_Flag[1]!=this._Training[a]._Term.Count)
{
A_Word.Add(this._Training[a]._Term[_Flag[1]]);
A_Term.Add(this._Training[a]._Value[_Flag[1]]);
_Flag[1]+=1;
}

我的整個程式執行到上面這段就會跳出
"索引超出範圍必須為非負數且小於集合的大小參數名稱index"

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

6
lordrd
iT邦新手 2 級 ‧ 2010-11-01 21:35:26
最佳解答

你的 _Testing 跟 _Training 是怎麼宣告的 ?
看起來 應該是死在這兩個物件上再使用 _Flag[x] 的時候

看更多先前的回應...收起先前的回應...
sacc2133 iT邦新手 5 級 ‧ 2010-11-01 21:38:35 檢舉
<pre class="c" name="code">Performance_Evaluation_kNN _Performance_Evaluation_kNN =
                        new Performance_Evaluation_kNN(
                            this._Training,
                            k,
                            Threshold,
                            "Chi-Square");
            foreach (var q in Directory.GetDirectories(Path.Combine(Path.Combine(Application.StartupPath, "Data"), "Category")))
            {
                DirectoryInfo _DirectoryInfo = new DirectoryInfo(q);
                foreach (var q2 in Directory.GetDirectories(Path.Combine(q, "Testing")))
                {
                    //--------------------------------------------------------------
                    List<String> _kNN_Category = new List<String>();
                    List<Double> _kNN_Value = new List<Double>();
                    Feature_kNN _Testing = new Feature_kNN(q2, _DirectoryInfo.Name);
}
}

上半段

lordrd iT邦新手 2 級 ‧ 2010-11-01 22:05:22 檢舉

若照你列出的資料來看
應該是你物件的"範圍"錯了~

你補充的 "上半段" 的資料的最後一行
_Testing 的存在周期 只有在 "這一行" 而已 !!!
出了 Foreach 的 大括弧 就沒了

你把 Feature_kNN _Testing = new Feature_kNN(q2, _DirectoryInfo.Name);
改成
_Testing = new Feature_kNN(q2, _DirectoryInfo.Name); 看看吧 !

因為 你列的不是很完整 就你所列的 大概的判斷 ~

sacc2133 iT邦新手 5 級 ‧ 2010-11-02 10:09:06 檢舉

我給完整的程式
http://xun6.com/file/9fa733786/Similarity.cs.html
private void chI_Square()//齊一性檢定
這一段

lordrd iT邦新手 2 級 ‧ 2010-11-02 11:49:49 檢舉

大哥 ...
不要考我啦 ~
直接跟我說 你是死在哪一行 ? 這樣比較快啦 ~

lordrd iT邦新手 2 級 ‧ 2010-11-02 12:01:50 檢舉

ㄟ 不用了
找到了 ~
你去看一下

for (Int32 a = 0; a != this._Training.Count; a += 1)
{
...
/*B_Word = null;
B_Term = null;
B_Word_num = 0;*/
_Flag = null;
}
這個迴圈的最後 你把 _Flag 設為 null 作啥
這樣只跑第一圈 就死了啦 ~

lordrd iT邦新手 2 級 ‧ 2010-11-02 12:18:10 檢舉

另外 幾個建議 給你參考 ~ 恩 ~ 僅供參考啦 ~

1.你程式的迴圈條件 盡量不要用直接用 !=
盡可能用 大於,小於 來作 ~ 這樣也增加可讀性與邏輯判讀
2.大迴圈內若有用到一些獨立區塊
把它獨立出來 作成小函式 呼叫
這樣也增加可讀性跟日後維護及除錯
3.雖然 C# 可以讓你隨地 宣告變數
但 建議還是在函式的開頭宣告 ~
不然 這樣 好像隨地大小便 ...
( ㄟ ... 似乎不雅 ... 但我覺得很貼切 ! 哈哈 ~ )

sacc2133 iT邦新手 5 級 ‧ 2010-11-02 20:49:09 檢舉

還是一樣耶!!

lordrd iT邦新手 2 級 ‧ 2010-11-03 09:07:33 檢舉

一樣 ?
所以你是死在第一圈 不是第二圈 ?
到底是死在哪一行 ?
哪個變數 ?
VS2008 以上的 debug 會指出是哪個變數啊 ! (2005 不記得可不可以 ~)
你有用 debug 查嗎 ?
你看你死掉的時候
相關的各個變數值的內容是多少 然後 用 call stack 反查
應該很快就能查出問題了
直接看 code 只能大概看流程問題
實際的數值變化導致的問題 沒用 debug ... 很難 ~

lordrd iT邦新手 2 級 ‧ 2010-11-03 09:12:06 檢舉

你檢查一下
while(_Flag[0]!=_Testing._Term.Count&&_Flag[1]!=this._Training[a]._Term.Count)
是不是無法滿足條件
一直 loop

sacc2133 iT邦新手 5 級 ‧ 2010-11-03 10:33:59 檢舉

昨晚試了一下發現
while(_Flag[0]!=_Testing._Term.Count&&_Flag[1]!=this._Training[a]._Term.Count)
與下面另兩個迴圈 只要是跑_Testing部分都不能跑

lordrd iT邦新手 2 級 ‧ 2010-11-04 14:49:01 檢舉

那你去查 Feature_kNN (...) 這個函式吧~
你的 _Testing 是他開出來的

sacc2133 iT邦新手 5 級 ‧ 2010-11-04 21:19:26 檢舉

我找到了 我改了 下面if裡面list的讀取方式!!

我要發表回答

立即登入回答