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