douqianrou9079 2013-06-02 11:58
浏览 26
已采纳

C#如何在锯齿状数组中创建字符串类型索引[关闭]

I have php code as below

   $array[$x] = array();
   $array[$x]["no"] = $no;
   $array[$x]["cid"] = $cid;
   $array[$x]["wait"] = $wait;
   $array[$x]["prio"] = $prio;
   $array[$x]["debug"] = $command[$i];

x++;

Question: I want to have similar code like above in C#, all is well but at the jagged array step, I am confused how to do same like this in C#, Can anyone help me out in this regard? Thats is index of an array is string value.

  • 写回答

1条回答 默认 最新

  • duanlu1950 2013-06-02 12:22
    关注

    One way of achieving a jagged array is a dictionary. Something based on the code below may get you started.

    Dictionary<string, string>[] array;
    
    void MyMethod(int[] ckeys, int gotovalue, string[] command)
    {
        int x = 0;
        for(int ii = (ckeys[0] + 1); ii < gotovalue; ii++)
        {
            string no = preg_replace(" .*", "", command[ii]); 
            string temp = preg_replace("^[0-9]*. ", "", command[ii]); 
            string cid = preg_replace(" (.*", "", temp); 
            temp = preg_replace(".* (wait: ", "", command[ii]); 
            string wait = preg_replace(",.*", "", temp); 
            temp = preg_replace(".*, prio: ", "", command[ii]); 
            string prio = preg_replace(").*", "", temp);
    
            array[x] = new Dictionary<string, string>();
            array[x]["no"] = no;
            array[x]["cid"] = cid;
            array[x]["wait"] = wait;
            array[x]["prio"] = prio;
            array[x]["debug"] = command[ii];
    
            x++;
        }
    }
    
    string preg_replace(string aa, string bb, string cc)
    {
        return aa + bb + cc;
    }
    

    Edit:

    I took the code in the initial version of the question and tried to convert it into C#, assuming that all the unspecified types were strings. The called routine preg_replace was not specified, but it appeared to take three strings and return one.

    The original question has the line $x = 0; which appears to define $x as an integer and initialize it. The line $array[$x] = array(); appears to say that $array at the given integer index is made to refer to an empty array. Then the line $array[$x]["no"] sets the "no" element of that array to a string. The C# I proposed declares array as an array of dictionaries. A C# dictionary is a form of associative array, in the Perl language it would be called a 'hash'. The whole piece of code will write values into the structure, effectively initializing it from the values found in the parameters to MyMethod.

    Elsewhere will need a statement such as array = new Dictionary<string, string>[gotovalue] to make array refer to an actual array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥15 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教