none
將資料放入jagged array RRS feed

  • 問題

  • $onehs,2505.249519,12134.007519,#

    onehs,2505.249519,12134.007519,#

    onehs,2505.249519,12134.007519,#

    onehs,2505.249519,12134.007519,#

    $onehs,2505.249519,55555.007519,#

    onehs,2505.249519,55555.007519,#

    onehs,2505.249519,55555.007519,#

    $onehs,2505.249519,33333.007519,#

    onehs,2505.249519,33333.007519,#

    onehs,2505.249519,33333.007519,#

    string[][] hs3 = new string[hs_num][];  //jagged array

    string[] temp = null;

    (1)如以上的資料,我要先將它以$為主分割

    string[] hs2 = hs1. Split(new char[] { '$' });

    (2)之後要將它再依逗號分割,並放入陣例裡

     for (int i = 0; i < hs2.Length; i++)
     {

            temp = hs2[ i ] .Split(new char[] { ',' });
            for (int j = 0; j < temp.Length; j++)
            {
                        hs3 [ i ] =new string []{temp[j]};
            }
      }

    但上面的紅色部份會出錯,訊息「錯誤 1 無法將型別 'string[]' 隱含轉換為 'string' D:\tt\Form1.cs 422 46 tt」

    我有試著把它改做hs3 [ i ]  [ j ]=temp[j]; 但也會出錯,訊息如「並未將物件參考設定為物件的執行個體」

    請問該如何解決呢?還是有建議其他的寫法,也可成為以下的結果!

    程式最後執行所要的結果如下(部份)

    h3[0]=onehs,2505.249519,12134.007519,onehs,2505.249519,12134.007519,onehs,2505.249519,12134.007519,onehs,2505.249519,12134.007519

    h3[1]=onehs,2505.249519,55555.007519,onehs,2505.249519,55555.007519,onehs,2505.249519,55555.007519

    h3[0][0]=onehs

    h3[0][1]=2505.249519

    2007年9月11日 上午 09:22

解答

  •  deli 寫信:

    $onehs,2505.249519,12134.007519,#

    onehs,2505.249519,12134.007519,#

    onehs,2505.249519,12134.007519,#

    onehs,2505.249519,12134.007519,#

    $onehs,2505.249519,55555.007519,#

    onehs,2505.249519,55555.007519,#

    onehs,2505.249519,55555.007519,#

    $onehs,2505.249519,33333.007519,#

    onehs,2505.249519,33333.007519,#

    onehs,2505.249519,33333.007519,#

    string[][] hs3 = new string[hs_num][];  //jagged array

    string[] temp = null;

    (1)如以上的資料,我要先將它以$為主分割

    string[] hs2 = hs1. Split(new char[] { '$' });

    (2)之後要將它再依逗號分割,並放入陣例裡

     for (int i = 0; i < hs2.Length; i++)
     {

            temp = hs2[ i ] .Split(new char[] { ',' });
            for (int j = 0; j < temp.Length; j++)
            {
                        hs3 [ i ] =new string []{temp[j]};
            }
      }

    但上面的紅色部份會出錯,訊息「錯誤 1 無法將型別 'string[]' 隱含轉換為 'string' D:\tt\Form1.cs 422 46 tt」

    我有試著把它改做hs3 [ i ]  [ j ]=temp[j]; 但也會出錯,訊息如「並未將物件參考設定為物件的執行個體」

    請問該如何解決呢?還是有建議其他的寫法,也可成為以下的結果!

    程式最後執行所要的結果如下(部份)

    h3[0]=onehs,2505.249519,12134.007519,onehs,2505.249519,12134.007519,onehs,2505.249519,12134.007519,onehs,2505.249519,12134.007519

    h3[1]=onehs,2505.249519,55555.007519,onehs,2505.249519,55555.007519,onehs,2505.249519,55555.007519

    h3[0][0]=onehs

    h3[0][1]=2505.249519

     

    以下是我大概改一下的Sample Code,FYI:

    Code Snippet

    string tmp = "onehs,2505.249519,12134.007519,#onehs,2505.249519,12134.007519,#onehs,2505.249519,12134.007519,#onehs,2505.249519,12134.007519,#$onehs,2505.249519,55555.007519,onehs,2505.249519,55555.007519,#onehs,2505.249519,55555.007519,#$onehs,2505.249519,33333.007519,#onehs,2505.249519,33333.007519,#onehs,2505.249519,33333.007519,#";

                string[] a1=tmp.Split("$".ToCharArray());
                string[][] b1=new string[a1.Length][];

                for (int i = 0; i < a1.Length; i++)
                {
                    string[] b2 = a1[i].Split(",".ToCharArray());
                    b1[i] = new string[b2.Length];
                    for (int z = 0; z < b2.Length; z++)
                    {
                        b1[i][z] = b2[z];
                    }
                }

     

     

    2007年9月11日 下午 01:56

所有回覆

  •  deli 寫信:

    $onehs,2505.249519,12134.007519,#

    onehs,2505.249519,12134.007519,#

    onehs,2505.249519,12134.007519,#

    onehs,2505.249519,12134.007519,#

    $onehs,2505.249519,55555.007519,#

    onehs,2505.249519,55555.007519,#

    onehs,2505.249519,55555.007519,#

    $onehs,2505.249519,33333.007519,#

    onehs,2505.249519,33333.007519,#

    onehs,2505.249519,33333.007519,#

    string[][] hs3 = new string[hs_num][];  //jagged array

    string[] temp = null;

    (1)如以上的資料,我要先將它以$為主分割

    string[] hs2 = hs1. Split(new char[] { '$' });

    (2)之後要將它再依逗號分割,並放入陣例裡

     for (int i = 0; i < hs2.Length; i++)
     {

            temp = hs2[ i ] .Split(new char[] { ',' });
            for (int j = 0; j < temp.Length; j++)
            {
                        hs3 [ i ] =new string []{temp[j]};
            }
      }

    但上面的紅色部份會出錯,訊息「錯誤 1 無法將型別 'string[]' 隱含轉換為 'string' D:\tt\Form1.cs 422 46 tt」

    我有試著把它改做hs3 [ i ]  [ j ]=temp[j]; 但也會出錯,訊息如「並未將物件參考設定為物件的執行個體」

    請問該如何解決呢?還是有建議其他的寫法,也可成為以下的結果!

    程式最後執行所要的結果如下(部份)

    h3[0]=onehs,2505.249519,12134.007519,onehs,2505.249519,12134.007519,onehs,2505.249519,12134.007519,onehs,2505.249519,12134.007519

    h3[1]=onehs,2505.249519,55555.007519,onehs,2505.249519,55555.007519,onehs,2505.249519,55555.007519

    h3[0][0]=onehs

    h3[0][1]=2505.249519

     

    以下是我大概改一下的Sample Code,FYI:

    Code Snippet

    string tmp = "onehs,2505.249519,12134.007519,#onehs,2505.249519,12134.007519,#onehs,2505.249519,12134.007519,#onehs,2505.249519,12134.007519,#$onehs,2505.249519,55555.007519,onehs,2505.249519,55555.007519,#onehs,2505.249519,55555.007519,#$onehs,2505.249519,33333.007519,#onehs,2505.249519,33333.007519,#onehs,2505.249519,33333.007519,#";

                string[] a1=tmp.Split("$".ToCharArray());
                string[][] b1=new string[a1.Length][];

                for (int i = 0; i < a1.Length; i++)
                {
                    string[] b2 = a1[i].Split(",".ToCharArray());
                    b1[i] = new string[b2.Length];
                    for (int z = 0; z < b2.Length; z++)
                    {
                        b1[i][z] = b2[z];
                    }
                }

     

     

    2007年9月11日 下午 01:56
  • 謝謝Jeff Yeh的幫助。不好意思我可以再請教另一個問題嗎???

    我有3個button,按下去後會執行同樣的方法(寫入檔案),寫入的內容分別是

    buttonA

    $onehs,2505.249519,12134.007519,#

    onehs,2505.249519,12134.007519,#

    onehs,2505.249519,12134.007519,#

    ........

     

    buttonB

    onea,2505.249519,12134.007519,#

    onea,2505.249519,12134.007519,#

    onea,2505.249519,12134.007519,#

    .........

     

    buttonC

    oneb,2505.249519,12134.007519,#

    oneb,2505.249519,12134.007519,#

    oneb,2505.249519,12134.007519,#

    ..........

     

    只有buttonA會有$這個字串,但不知道是不是buttonA的內容還沒寫完,

    我就按了buttonB,導致資料會錯誤,$會亂跑,例如下面這樣

    buttonA

    onehs,2505.249519,12134.007519,#

    onehs,2505.249519,12134.007519,#

    onehs,2505.249519,12134.007519,#

    ........

     

    buttonB

    onea,2505.249519,12134.007519,#

    $onea,2505.249519,12134.007519,#

    onea,2505.249519,12134.007519,#

    .........

     

    buttonC

    oneb,2505.249519,12134.007519,#

    oneb,2505.249519,12134.007519,#

    oneb,2505.249519,12134.007519,#

    ..........

    我是使用if(int a==0)一個變數去控制字串是否會有$,使用 FileStream ,方法最後面會Flush及Close

    那為何資料還會亂跑呢?

    2007年9月12日 下午 02:24
  • 這個沒看程式,很難說一個所以然來.

     

    建議用Debug的方式,去看寫入時,是寫入什麼資料,說不定寫入前,資料就有問題.

    檢查一下是否有那個地方的邏輯有問題.

     

    這是給你的大概建議~

     

    2007年9月13日 上午 12:32
  • 我有試著去Debug過,不過一直找不到原因在哪???我將我的程式貼上來,麻煩幫我看看好嗎?

    int h_control,local_ctrl=0;

     

    private void bs_Click(object sender, EventArgs e) //不要記錄$
    {
                local_ctrl = 1;
                h_control = 1;
                wr_txt(all);
     }

     

    private void h2_Click(object sender, EventArgs e) //要記錄$
    {
                local_ctrl = 0;
                wr_txt(all);
    }

     

    private void wr_txt(string all_temp)
            {
                FileStream fs = null;
                string localt = local.Text;
                if (all_temp != null)
                {
                    string[] records = all_temp.Split(new char[] { '#' });
                    string s = "";

                    try
                    {
                        switch (localt) 
                        {
                            case "東":
                                fs = new FileStream(route1 + "\\"  + course + "_東" + route2, FileMode.Open);
                                for (int i = 0; i < records.Length-1; i++)
                                {
                                    if (local_ctrl == 0 )
                                    {
                                        if (h_control == 0 )
                                        {
                                            fs.Seek(0, SeekOrigin.End);
                                            s = "$" + recordsIdea + "#" + "\n";
                                            fs.Write(encoding.GetBytes(s), 0, s.Length);
                                            h_control = 1;
                                        }
                                        else if(local_ctrl ==1)
                                        {
                                            fs.Seek(0, SeekOrigin.End);
                                            s = recordsIdea + "#" + "\n";
                                            fs.Write(encoding.GetBytes(s), 0, s.Length);
                                        }
                                        local_ctrl = 1;
                                    }
                                    else if(local_ctrl ==1)
                                    {
                                        fs.Seek(0, SeekOrigin.End); //內容的結尾
                                        s = recordsIdea + "#" + "\n";
                                        fs.Write(encoding.GetBytes(s), 0, s.Length);
                                    } 
                                }
                                break;

                            case "西":

                                ...............................與東的語法一樣,只是寫入不同的檔案

                        }
                        fs.Flush();
                        fs.Close();
                        MessageBox.Show("儲存成功!!!");
                       
                    }
                    catch
                    {
                        MessageBox.Show("檔案寫入未成功,請重新記錄!!");
                    }
                }
                else
                {
                    MessageBox.Show("定位未成功,請重新記錄!!");
                }
            }

     

    我確定寫入的格式是對的,因為如果單單按各button,不一起按的話,記錄的格式就是對的,所以我才想是不是

    因為buttonA的資料還未完全寫入檔案,就按了buttonB導致$就寫入不該寫的地方!

    2007年9月13日 上午 06:39
  •   switch (localt) 
                        {
                            case "東":
                                fs = new FileStream(route1 + "\\"  + course + "_東" + route2, FileMode.Open);
                                for (int i = 0; i < records.Length-1; i++)
                                {
                                    if (local_ctrl == 0 )
                                    {
                                        if (h_control == 0 )
                                        {
                                            fs.Seek(0, SeekOrigin.End);
                                            s = "$" + records + "#" + "\n";
                                            fs.Write(encoding.GetBytes(s), 0, s.Length);
                                            h_control = 1;
                                        }
                                        else if(local_ctrl ==1)
                                        {
                                            fs.Seek(0, SeekOrigin.End);
                                            s = records + "#" + "\n";
                                            fs.Write(encoding.GetBytes(s), 0, s.Length);
                                        }
                                        local_ctrl = 1;
                                    }
                                    else if(local_ctrl ==1)
                                    {
                                        fs.Seek(0, SeekOrigin.End); //內容的結尾
                                        s = records + "#" + "\n";
                                        fs.Write(encoding.GetBytes(s), 0, s.Length);
                                    } 
                                }

                                fs.Flush();<==

                                fs.Close();<==
                                break;

                            case "西":

                                ...............................與東的語法一樣,只是寫入不同的檔案

                        }
    //                    fs.Flush();<===因為你fs都用同一個,我是沒試,純猜測.把這兩個移到"東"西"裡去試看看.
    //                    fs.Close();<===如果還是不行,再試各自去New一個不同的FileStream看看.不要"東","西"都是用同一個fs來做寫入.

                        MessageBox.Show("儲存成功!!!");

     

    2007年9月13日 上午 08:52
  •  

    兩個方法我都試過了,不行…

    另外,我是寫到同一個檔,東、西是不同的檔,目前存在的問題是

    如果我寫到東這個檔,他就會錯了…

    2007年9月13日 下午 01:44
  • 另外,我是寫到同一個檔,東、西是不同的檔

    (??那到底是同一個檔,還是不同的檔??)

     

    目前存在的問題是如果我寫到東這個檔,他就會錯了…

    這意思就是,如果寫到西這個檔就不會?如果步驟,順序什麼都一樣?

    如果是東,就"一定"會發生?

    2007年9月14日 上午 01:50
  • 不好意思,我解釋的不清楚…

    東是一個檔,西是一個檔,他們寫入的內容都像上面那樣的格式

    但因程式需要,所以要分成兩個檔!

    目前我只將資料寫入東這個檔案,因為如果東成功,那西也就成功了!

    不過目前就是資料會出錯的問題…

    麻煩大大幫我看看,謝謝!

    2007年9月15日 上午 03:55