“在[]中错误的索引数量,预期”2“错误

时间:2014-02-20 15:34:06

标签: c#

我读了类似的答案,但我无法得到答案。

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace function
{
    public partial class Form1 : Form
    {
        float userscore,itemscore;
            string lineitem, lineuser;
            float[,] a = new float[89395, 100];
            float[,] b = new float[1143600, 100];
            //float[,] c = new float[89395, 100];

            StreamReader fileitem = new StreamReader("c:\\1.txt");
            StreamReader fileuser = new StreamReader("c:\\2.txt");
        public Form1()
        {
            InitializeComponent();

             for (int x = 0; x <= 8939500; x++)
            {
                lineuser = fileuser.ReadLine();
                string[] values = lineuser.Split(' ');
                int userid, factoriduser;
                foreach (string value in values)
                {
                    userid = Convert.ToInt32(values[0]);
                    factoriduser = Convert.ToInt32(values[1]);
                    userscore = Convert.ToSingle(values[2]);
                    a[userid,factoriduser] = userscore;
                }
            }

            for (int y = 0; y <= 114360000; y++)
            {
                lineitem = fileitem.ReadLine();
                string[] valuesi = lineitem.Split(' ');
                int itemid, factoriditem;
                foreach (string value in valuesi)
                {
                    itemid = Convert.ToInt32(valuesi[0]);
                    factoriditem = Convert.ToInt32(valuesi[1]);
                    itemscore = Convert.ToSingle(valuesi[2]);
                    b[itemid,factoriditem] = itemscore;
                }

            }

        }
        public float dotproduct(int userid,int itemid)
        {
            float result;
            //get the score of 100 from user and item to dotproduct
            float[] u_f = a[userid];
            float[] i_f = b[itemid];

            for (int i = 0; i <u_f.GetLength(0) ; i++)
            {
                result += u_f[userid,i] * i_f[itemid,i];
            }
            return result;

        }

        private void btn_recomm_Click(object sender, EventArgs e)
        {
           int sc = Convert.ToInt32(txtbx_id.Text);
           if (sc>=0 &&sc<=89395)
             {
              for (int z=0;z<=1143600;z++)
                {
                  dotproduct(sc,z);
                }
             }
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.Close();
            MessageBox.Show("Obrigado !");
        }

        private void btn_reset_Click(object sender, EventArgs e)
        {
            txtbx_id.Clear();
            txtbx_itemid.Clear();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}

此外,如果我想从文本框中读取变量(sc位于下方)并将其与

匹配
int sc = Convert.ToInt32(txtbx_id.Text);

if (sc>=0 &&sc<=89395)
{
    for (int z=0;z<=1143600;z++)
    {
        dotproduct;
    }
}

存储第一个数据集第一列的userid = Convert.ToInt32(values[0]);中的用户ID,我必须再次读取第一个数据集才能找到特定的用户ID或者可以使用其他技术吗?

1 个答案:

答案 0 :(得分:0)

你有几个问题

  1. dotproduct声明为void,但仍尝试返回值。
  2. dotproduct有2个参数,但你试图在没有参数的情况下调用它。
  3. 您正在尝试提取二维数组的一个“维度”
  4. (一旦修复3,1和2将成为编译器错误。)

    您可以将ab更改为“锯齿状”数组(int[][]),也可以只使用循环中的列索引:

    public void dotproduct(int userid,int itemid)
    {
        float result;
        //get the score of 100 from user and item to dotproduct
    
        for (int i = 0; i < a.GetLength(1); i++)   // notice the change from <= to < to avoid out-of-bounds exception
        {
            result += a[userid,i] * b[itemid,i];
        }
        return result;  // cannot "return" a result from a void function - change the declaration
    
    }