【问题标题】:Index was outside the bounds of array索引超出了数组的范围
【发布时间】:2014-03-21 15:11:02
【问题描述】:

当我尝试运行应用程序时,它会在 float[] u_f = a[userid]; 行显示 Index was outside the bounds of the array 任何想法? PS。用户 ID 可以是每个整数,但我取整数的索引,介于 (0, 1143600 for item) 和 (0, 89395 for user) 之间,我的计算基于此。然后,我的计算基于userid 值的索引,它存储在数组 a 中,而不是基于 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);
            }

【问题讨论】:

  • 错误说明了一切,单步执行您的代码 - a[userid] 在数组之外,因此例如,如果您有 0 - 4 索引并且 userid 高于 4,它会抛出例外。
  • userid的值是多少?
  • 出现错误是因为userid值等于或大于数组长度。
  • 您的数组有二维 float[][] a 但在错误行中您尝试仅找到一维 float[] u_f = a[userid];
  • 我编辑了我的代码并添加了一些解释,请你再查一下好吗? @SonerGönül

标签: c# .net arrays indexoutofrangeexception


【解决方案1】:

表示userID的值大于数组a的最大索引号。最大索引为 count - 1。

错误消息提到了这一点。

另外,看起来a 是二维的。这可能是你的问题吗?

【讨论】:

  • 用户 ID 可以是每一个整数,但我取整数的索引,介于 (0, 1143600 for item) 和 (0, 89395 for user) 之间,我的计算是基于此。然后,我的计算是基于存储在数组 a 中的 userid 值的索引,而不是基于 userid 的值。
【解决方案2】:

显然dotproduct 被调用时userid 的值等于或大于a.Length。

如果这不应该发生,那么在声明 u_f 数组之前添加这一行:

Debug.Assert(a.Length > userid);

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

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

【讨论】:

  • 用户 ID 可以是每一个整数,但我取整数的索引,介于 (0, 1143600 for item) 和 (0, 89395 for user) 之间,我的计算是基于此。然后,我的计算是基于存储在数组 a 中的用户 ID 值的索引,而不是基于用户 ID @crono 的值
【解决方案3】:

将 [userid] 替换为

 a[userid-1]

【讨论】:

  • 我们甚至不知道 userid 的值。
猜你喜欢
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
  • 2015-11-06
  • 2010-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多