[英]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];
当我尝试运行应用时,它显示Index was outside the bounds of the array
在float[] u_f = a[userid];
行Index was outside the bounds of the array
float[] u_f = a[userid];
Any idea? 任何想法? PS. 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
. 用户ID可以是每个整数,但我采用的整数索引介于(0,1143600表示项目)和(0,89395表示用户)之间,我的计算基于此。然后,我的计算基于的索引userid
值存储在阵列中的不基于所述值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
. 显然,以userid
值等于或大于a.Length
调用dotproduct
。
If that's not supposed to happen then add this line before declaring u_f
array: 如果不应该发生这种情况,请在声明u_f
数组之前添加以下行:
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
. 这意味着userID
的值高于数组a
的最大索引号。 The maximum index is count - 1. 最大索引为count-1。
The error message mentions that. 错误消息中提到了这一点。
Also, it looks like a
is two dimensional. 此外,它看起来像a
是二维的。 Could this be your problem? 这可能是您的问题吗?
用[userid]替换
a[userid-1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.