2014-02-21 1 views
1

질문을하기 전에 이전 게시물을 읽었습니다. 스크립트를 실행하면 다음 코드에 Invalid rank specifier:expected ',' or ']' 오류가 표시됩니다. BTW, 내가 시도한 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

밤은 그것이 '떠 [] A = 부동 소수점 [X, y]는'- http://msdn.microsoft.com/en- us/library/2yd9wwz4.aspx – tymeJV

답변

1

당신은 모든 내부 배열 선언 후 첫 외부 배열을 선언하고해야 그런 식으로 새로운 2D 배열을 만들 수는 없습니다. 한 번에 하나의 차원을 만듭니다. 두 번째 dimensiuon를 초기화 루프 useva 또는 사용할 수 LINQ :

float[][] a = Enumerable.Range(0, 89395).Select(i=>new float[100]).ToArray(); 
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]; 
+0

float [,] ..... 다음 형식을 사용하고 싶다면 ..... @ marcinjuraszek – user3328039

+0

내부 배열에 다른 치수가 필요하지 않습니다.)'[,]'와 함께 가야한다. 왜냐하면 관리가 쉽기 때문이다. – MarcinJuraszek

3
float[,] a = new float[89395,100]; 
float[,] b = new float[1143600,100]; 

참조 : http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

+0

이전에 사용했는데, "[[]에서 인덱스가 잘못되어 줄 서서 '2' '라고 예상했습니다. ------ float [] u_f = a [userid]; – user3328039

+0

또한 다차원 배열을 사용하려면 다른 코드를 수정해야합니다. – Andy

관련 문제