在我提出问题之前,我已阅读上一篇文章。当我运行脚本时,它会在以下代码中显示Invalid rank specifier:expected ',' or ']'
错误。顺便说一句,我试过new float[8939, 100];
,但它仍然显示错误。
还有什么可以使用哈希表来保存我写哈希表注释的结果。
namespace function
{
public partial class Form1 : Form
{
float userscore,itemscore,result;
string lineitem, lineuser;
float[][] a = new float[89395][100]; //<----the error is here
float[][] b = new float[1143600][100]; //<----the error is here
//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)
{
//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(1); i++)
{
result += u_f[userid] * i_f[itemid];
}
return result;
}
private void btn_recomm_Click(object sender, EventArgs e)
{
if(txtbx_id.Text==null)
{
MessageBox.Show("please insert user id");
}
if (txtbx_id.Text != null)
{
int sc = Convert.ToInt32(txtbx_id.Text);
if (sc>=0 &&sc<=89395)
{
for (int z=0;z<=1143600;z++)
{
dotproduct(sc,z);
}
//Hashtable hashtable = new Hashtable();
//put the result in hashtable
//foreach (DictionaryEntry entry in hashtable)
//{
//Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
// }
}
}
if (txtbx_id==null &&txtbx_itemid==null)
{
int uid = Convert.ToInt32(txtbx_id.Text);
int iid = Convert.ToInt32(txtbx_itemid.Text);
{
if (uid>=0 && uid<=89395 && iid>=0 && iid<=1143600)
{
dotproduct(uid,iid);
MessageBox.Show("The Score of user id "+uid+" is "+result);
}
}
}
}
答案 0 :(得分:4)
你不能这样声明jagged array。您必须先声明外部数组,然后声明所有内部数组:
float[][] a = new float[89395][];
for(int i = 0; i < 89395; i++)
a[i] = new float[100];
或者您应该将数组更改为多维float[,]
数组:
float[,] a = new float[89395,100];
答案 1 :(得分:3)
float[,] a = new float[89395,100];
float[,] b = new float[1143600,100];
答案 2 :(得分:1)
你不能制作一个像这样的新2D阵列 - 你一次只做一个维度。你可以用useva循环来初始化第二个维度,或者使用LINQ:
float[][] a = Enumerable.Range(0, 89395).Select(i=>new float[100]).ToArray();