2014-02-22 40 views
-1

當我運行腳本時,出現「索引在數組邊界之外」錯誤,線路爲float[] i_f = b[itemid];「索引超出了數組邊界」乘法2個矩陣的錯誤

BTW,我改變

for (int y = 0; y <= 114360000; y++)for (int y = 0; y < 114360000; y++)

for (int x = 0; x <= 8939500; x++)for (int x = 0; x < 8939500; x++)

,但它再次顯示了同樣的錯誤。

供參考:該代碼有兩個部分:即得到2點矩陣100倍的值和它們相乘(矩陣乘法),其正常工作

第一部分。

重複第一部分1143600次的第二部分(運行此部分時發生錯誤)。

namespace function 
{ 
    public partial class Form1 : Form 
    { 
     //Hashtable hashtable = new Hashtable(); 
     float userscore, itemscore,result; 
     string lineitem, lineuser; 
     //float[][] a = new float[89395][100]; 
     //float[][] b = new float[1143600][100]; 
     float[][] a = Enumerable.Range(0, 89395).Select(i => new float[100]).ToArray(); 
     float[][] b = Enumerable.Range(0, 1143600).Select(j => new float[100]).ToArray(); 
     //float[,] c = new float[89395, 100]; 
     StreamReader fileitem = new StreamReader("c:\\p.txt"); 
     StreamReader fileuser = new StreamReader("c:\\L.txt"); 
     public Form1() 
     { 
      InitializeComponent(); 
      for (int x = 0; x <= 8939500; x++) 
      { 
       lineuser = fileuser.ReadLine(); 
       if (!string.IsNullOrEmpty(lineuser)) 
       { 
        string[] values = lineuser.Split(' '); 
        int userid, factoriduser; 
        foreach (string value in values) 
        { 
         userid = Convert.ToInt32(values[0]); 
         factoriduser = Convert.ToInt32(values[1]); 
         userscore = Convert.ToSingle(values[2]); 
         a[userid][factoriduser] = userscore; 
        } 
       } 
      } 

      for (int y = 0; y <= 114360000; y++) 
      { 
       lineitem = fileitem.ReadLine(); 
       if (!string.IsNullOrEmpty(lineitem)) 
       { 
        string[] valuesi = lineitem.Split(' '); 
        int itemid, factoriditem; 
        foreach (string value in valuesi) 
        { 
         itemid = Convert.ToInt32(valuesi[0]); 
         factoriditem = Convert.ToInt32(valuesi[1]); 
         itemscore = Convert.ToSingle(valuesi[2]); 
         b[itemid][factoriditem] = itemscore; 
        } 
       } 

      } 

     } 
     public float dotproduct(int userid, int itemid) 
     { 

      //get the score of 100 from user and item to dotproduct 
      //get userid and itemid element from array a and b 
      float[] u_f = a[userid]; 
      float[] i_f = b[itemid]; //<-----error happens 

      for (int i = 0; i < u_f.Length; i++) 
      { 
       // result += u_f[userid] * i_f[itemid]; 
       result += u_f[i] * i_f[i]; 

      } 
      return result; 

     } 

     private void btn_recomm_Click(object sender, EventArgs e) 
     { 
      // if (txtbx_id.Text ==null) 
      if (String.IsNullOrEmpty(txtbx_id.Text)) 
      { 
       MessageBox.Show("please insert user id"); 
      } 
      //if (txtbx_id.Text != null && txtbx_itemid == null) 
       if (!String.IsNullOrEmpty(txtbx_id.Text) && String.IsNullOrEmpty(txtbx_itemid.Text)) 
      { 
       int sc = Convert.ToInt32(txtbx_id.Text); 


       if (sc >= 0 && sc <= 89395) 
       { 
        //for (int z = 0; z <= 1143600; z++) 
        //{ 
         // dotproduct(sc, z); 
         // hashtable.Add(z, result); 
        //} 
        //SortedDictionary<int, float> dict = new SortedDictionary<int, float>(hashtable); 
        //foreach (int key in dict) 
        //{ 
         // System.Console.WriteLine(String.Format("{0}: {1}", key, dict[key])); 
        //} 
        var results = new List<float>(1143600); 
        for (int z = 0; z <= 1143600; z++) 
        { 
         results.Add(dotproduct(sc, z)); 
        } 
        foreach (var resultwithindex in results.Select((r, index) => new { result = r, Index = index }).OrderByDescending(r => r.result).Take(10)) 
        { 
         System.Console.WriteLine(string.Format("{0}: {1}", resultwithindex.Index, resultwithindex.result)); 
        } 
        } 



      } 
      //if (txtbx_id!=null && txtbx_itemid!=null) 
      if (!String.IsNullOrEmpty(txtbx_id.Text) && !String.IsNullOrEmpty(txtbx_itemid.Text)) 
      { 
       int uid = Convert.ToInt32(txtbx_id.Text); 
       // int uid; 
       // if (!Int32.TryParse(txtbx_id.Text, out uid)) 
       // { 
       // MessageBox.Show("Try again"); 
       // } 

       int iid = Convert.ToInt32(txtbx_itemid.Text); 
       // int iid; 
       //if (!Int32.TryParse(txtbx_id.Text, out iid)) 
       //{ 
       //MessageBox.Show("Try again"); 
       //} 
       //{ 
       if (uid >= 0 && uid <= 89395 && iid >= 0 && iid <= 1143600) 
       { 
        dotproduct(uid, iid); 
        MessageBox.Show("The Score of item id " + iid + " is " + result); 
       } 
      } 
     } 
+2

請簡化代碼到一個易於遵循的例子,說明了一個問題,就是太多的代碼,而調試器遵循徹底。 –

回答

0

問題:您已聲明數組a大小爲89395b大小爲1143600,並試圖從陣列a1143600個元素從數組b訪問89395個元素。

解決方案:您應該始終記住,數組索引始終始於zero並以Length-1結尾。

所以你只能從陣列a訪問從0-89394的元素和
你只能從0-1143599從陣列b訪問的元素。

所以你需要相應地改變你的for-loop。

替換此:

if (uid >= 0 && uid <= 89395 && iid >= 0 && iid <= 1143600) 
{ 
    dotproduct(uid, iid); 
    MessageBox.Show("The Score of item id " + iid + " is " + result); 
} 

這一點:

if (uid >= 0 && uid < 89395 && iid >= 0 && iid < 1143600) 
{ 
    dotproduct(uid, iid); 
    MessageBox.Show("The Score of item id " + iid + " is " + result); 
} 

解決方案2:

替換此:

if (sc >= 0 && sc <= 89395) 

與此:

if (sc >= 0 && sc < 89395) 
+0

謝謝@SudhakarTillapudi。非常有用的信息「數組索引總是從零開始,以長度爲1結束」。但不幸的是,它顯示相同的 – user3328039

+0

@ user3328039:檢查我編輯的答案'解決方案2' –

+0

我試過了@SudhakarTillapudi。 。它必須是正確的,因爲它不會再顯示錯誤。但代碼沒有顯示任何內容(錯誤或輸出)。 – user3328039

相關問題