简体   繁体   中英

Index was outside the bounds of array

When I try run the aplication, it shows the Index was outside the bounds of the array at line float[] u_f = a[userid]; Any idea? PS. the user ID can be every integer,but I take the index of the integer with is between(0, 1143600 for item) and (0, 89395 for user) and my calculation is based on that.Then, my calculation is based on the index of userid value which is stored in array a not based on the value of userid . Thanks in advance

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

Obviously dotproduct gets called with userid value being equal to or greater than a.Length .

If that's not supposed to happen then add this line before declaring u_f array:

Debug.Assert(a.Length > userid);

Of course this won't solve the problem in itself but it will ensure you that whenever such a situation happens while you are testing, it won't go unnoticed or swallowed.

As a side note, clearer variable names would make it easier on you to read your code and figure out issues. Using actual types instead of jagged arrays would likely help too, if possible.

It means the value of userID is higher than the maximum index number of the array a . The maximum index is count - 1.

The error message mentions that.

Also, it looks like a is two dimensional. Could this be your problem?

用[userid]替换

 a[userid-1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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