乘法2矩阵中的“索引超出了数组的范围”错误

时间:2014-02-22 21:09:42

标签: c#

当我运行脚本时,我在第float[] i_f = b[itemid];行得到“索引超出数组范围”错误。

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);
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

问题:您已声明大小为a89395且大小为b且尝试访问1143600的数组89395数组a中的元素和数组1143600中的b元素。

解决方案:您应该始终记住,数组索引始终以zero开头,以Length-1结尾。

因此,您只能从数组a
中访问0-89394中的元素 您只能从数组b访问0-1143599中的元素。

所以你需要相应地更改你的for循环。

替换它:

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)
相关问题