C# 返回-1作为变量的值;

C# 返回-1作为变量的值;,c#,.net,arrays,C#,.net,Arrays,当我尝试运行该应用程序时,它显示索引在float[]u_f=a[userid]行的数组边界之外;当我检查userid的值时,它是-1; 有什么想法吗? PS.用户ID可以是每个整数,但我将整数的索引设置为(项目为0,1143600)和(用户为0,89395),我的计算基于此。然后,我的计算基于存储在数组a中的用户ID值的索引,而不是基于用户ID的值。 提前谢谢 float[][] a = Enumerable.Range(0, 89395).Select(i => new

当我尝试运行该应用程序时,它显示索引在float[]u_f=a[userid]行的数组边界之外;当我检查userid的值时,它是-1; 有什么想法吗? PS.用户ID可以是每个整数,但我将整数的索引设置为
(项目为0,1143600)
(用户为0,89395)
,我的计算基于此。然后,我的计算基于存储在数组a中的
用户ID
值的索引,而不是基于
用户ID
的值。 提前谢谢

        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-The value of user id is -1)
            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);
            }
float[]a=Enumerable.Range(089395)。选择(i=>newfloat[100]).ToArray();
float[]b=Enumerable.Range(01143600)。选择(j=>newfloat[100]).ToArray();
int[]c=新int[1258038];
int[]d=新int[92160];
........
公共浮动点产品(int userid、int itemid)
{
结果=0f;
float[]u_f=a[userid];//r.result.Take(n))
{
AppendFormat(“{0}:{1}”,d[resultwithindex.Index],resultwithindex.result);
sb1.AppendLine();
}
Show(sb1.ToString());
}
如果(!String.IsNullOrEmpty(txtbx_id.Text)和&!String.IsNullOrEmpty(txtbx_itemid.Text)和&String.IsNullOrEmpty(txtbx_noofrecomm.Text))
{
int uid=Convert.ToInt32(txtbx_id.Text);
int iid=转换为32(txtbx_itemid.Text);
int userseq0=Array.IndexOf(d,uid);
int itemseq0=Array.IndexOf(c,iid);
dotproduct(userseq0、itemseq0);
MessageBox.Show(“项目id”+itemseq0+“的得分为”+result);
}

我不知道你在问什么。您有一个错误,因为您试图访问a的索引-1。数组从0开始。值-1不存在
userid
应该介于0和a.Length之间。

我不确定你在问什么。您有一个错误,因为您试图访问a的索引-1。数组从0开始。值-1不存在
userid
应该介于0和a.Length之间。

您的问题是这行:

int userseq=Array.IndexOf(d, sc);
大概是返回
-1
,然后将其传递到dotproduct函数中,然后导致其失败。您需要决定在
d
中找不到
sc
的情况下应用什么逻辑,并实现它


尽管正如其他人所说,验证您对dotproduct的输入将帮助您更容易地发现问题。

您的问题是这一行:

int userseq=Array.IndexOf(d, sc);
int userseq=Array.IndexOf(d, sc);
大概是返回
-1
,然后将其传递到dotproduct函数中,然后导致其失败。您需要决定在
d
中找不到
sc
的情况下应用什么逻辑,并实现它

尽管正如其他人所说,验证您对dotproduct的输入将帮助您更容易地发现问题

int userseq=Array.IndexOf(d, sc);
如果数组
d
中不存在项
sc
,则返回-1。然后将
userseq
变量作为函数
dotproduct
的第一个参数传递,并根据其第一个参数(
userid
)获取数组索引。这就是你的问题所在-在的
索引中

我不知道如何修复它-这是您的构建逻辑,您必须决定如何处理数组中不存在输入数据的情况。可能会立即向用户显示消息

如果数组
d
中不存在项
sc
,则返回-1。然后将
userseq
变量作为函数
dotproduct
的第一个参数传递,并根据其第一个参数(
userid
)获取数组索引。这就是你的问题所在-在
索引中

我不知道如何修复它-这是您的构建逻辑,您必须决定如何处理数组中不存在输入数据的情况。可能会立即向用户显示消息。

您的方法是否应该接受低于零的
用户ID
值?如果是这样,则不能使用它来定义数组的长度。句号

如果-1不是一个有效值,我会在
dotproduct
方法的第一行添加它,因为它是公共的:

if (userid < 0) throw new ArgumentOutOfRangeException("userid");
if(userid<0)抛出新ArgumentOutOfRangeException(“userid”);
至于为什么在当前上下文中它是-1,正如其他人指出的那样,如果您试图在
d
数组中查找的文本值不存在,那么
IndexOf
方法将返回-1。在调用方法之前,必须验证用户的输入

我还要说我在你的另一篇文章中已经告诉过你的:用一种清晰的方式命名变量,如果这在你的场景中有意义的话,使用自定义类而不是交错数组。您的代码以后将更易于阅读和维护。

您的方法是否应该接受低于零的
用户ID
值?如果是这样,则不能使用它来定义数组的长度。句号

如果-1不是一个有效值,我会在
dotproduct
方法的第一行添加它,因为它是公共的:

if (userid < 0) throw new ArgumentOutOfRangeException("userid");
if(userid<0)抛出新ArgumentOutOfRangeException(“userid”);
至于为什么在当前上下文中它是-1,正如其他人指出的那样,如果您试图在
d
数组中查找的文本值不存在,那么
IndexOf
方法将返回-1。在调用方法之前,必须验证用户的输入


我还要说我在你的另一篇文章中已经告诉过你的:用一种清晰的方式命名变量,如果这在你的场景中有意义的话,使用自定义类而不是交错数组。以后您的代码将更易于阅读和维护。

您的问题令人困惑。不要试图破坏您自己的问题。这是不合适的行为。你的问题让人困惑。不要试图打断我