繁体   English   中英

索引超出数组范围

[英]Index was outside the bounds of array

当我尝试运行应用时,它显示Index was outside the bounds of the arrayfloat[] u_f = a[userid];Index was outside the bounds of the array float[] u_f = a[userid]; 任何想法? PS。 用户ID可以是每个整数,但我采用的整数索引介于(0,1143600表示项目)和(0,89395表示用户)之间,我的计算基于此。然后,我的计算基于的索引userid值存储在阵列中的不基于所述值userid 提前致谢

        float[][] a = Enumerable.Range(0, 89395).Select(i => new float[100]).ToArray();
        float[][] b = Enumerable.Range(0, 1143600).Select(j => new float[100]).ToArray();
        int[] c = new int[1258038];
        int[] d = new int [92160];
        ........
        public float dotproduct(int userid, int itemid)
        {
            result = 0f;
            float[] u_f = a[userid];   //  <----Error Line (index was outside the bounds of array)
            float[] i_f = b[itemid];

            for (int i = 0; i < u_f.Length; i++)
            {
                result += u_f[i] * i_f[i];
            }
            return result;
        }
        private void btn_recomm_Click(object sender, EventArgs e)
        {

            if (!String.IsNullOrEmpty(txtbx_id.Text) && String.IsNullOrEmpty(txtbx_itemid.Text) && !String.IsNullOrEmpty(txtbx_noofrecomm.Text))
            {
                    int sc = Convert.ToInt32(txtbx_id.Text);
                    int n = Convert.ToInt32(txtbx_noofrecomm.Text);
                    int userseq=Array.IndexOf(d, sc);
                    var results = new List<float>(1143600);
                    for (int z = 0; z <= 1143600; z++)
                    {
                        results.Add(dotproduct(userseq, z));
                    }
                    var sb1 = new StringBuilder();
                    foreach (var resultwithindex in results.Select((r, index) => new { result = r, Index = index }).OrderByDescending(r => r.result).Take(n))
                    {
                        sb1.AppendFormat("{0}: {1}", d[resultwithindex.Index], resultwithindex.result);
                        sb1.AppendLine();
                    }
                    MessageBox.Show(sb1.ToString());


            }
            if (!String.IsNullOrEmpty(txtbx_id.Text) && !String.IsNullOrEmpty(txtbx_itemid.Text) && String.IsNullOrEmpty(txtbx_noofrecomm.Text))
            {
                int uid = Convert.ToInt32(txtbx_id.Text);
                int iid = Convert.ToInt32(txtbx_itemid.Text);
                int userseq0 = Array.IndexOf(d, uid);
                int itemseq0 = Array.IndexOf(c, iid);
                dotproduct(userseq0, itemseq0);
                MessageBox.Show("The Score of item id " + itemseq0 + " is " + result);
            }

显然,以userid值等于或大于a.Length调用dotproduct

如果不应该发生这种情况,请在声明u_f数组之前添加以下行:

Debug.Assert(a.Length > userid);

当然,这本身并不能解决问题,但可以确保您在进行测试时,无论何时发生这种情况,都不会被忽视或吞咽。

附带说明一下,更清晰的变量名将使您更容易阅读代码并找出问题。 如果可能的话,使用实际类型而不是锯齿状的数组也可能会有所帮助。

这意味着userID的值高于数组a的最大索引号。 最大索引为count-1。

错误消息中提到了这一点。

此外,它看起来像a是二维的。 这可能是您的问题吗?

用[userid]替换

 a[userid-1]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM