Array.Copy并使用变量访问字符串数组

时间:2015-10-29 19:21:49

标签: c#

我尝试了几件事,但似乎无法得到这个。我有10个MotorReply字符串,然后我分成数组元素。然后我想将这些元素复制到另一个数组,这样我可以再循环,但无论我尝试什么,我都无法通过使用递增i变量访问BayReplyArray,即BayReplyArray [i]

声明:

    string[] MotorReplyArray = new string[30];
    string[] BayReplyArray1 = new string[30];
    string[] BayReplyArray2 = new string[30];
    string[] BayReplyArray3 = new string[30];
    up to 10

    int j = 0;

     for (int i = 1; i < 11; i++)
     {
        //  here we take the Motor? reply string for each bay and split the parameters into individual string arrays
        char[] delimiters = new char[] { '\r', ':' };
        MotorReplyArray = MotorReply[i].Split(delimiters);
        foreach (string line in MotorReplyArray)
        {
           // trim whitespace from ends
           MotorReplyArray[j] = line.Trim();
           j++;
        }
        Array.Copy(MotorReplyArray, BayReplyArray[i], j);
        Array.Clear(MotorReplyArray, 0, j);
        j = 0;
     }

1 个答案:

答案 0 :(得分:1)

  

我无法使用递增i变量访问BayReplyArray,即BayReplyArray [i]

您似乎认为如果i1,那么BayReplyArray[i]BayReplyArray1相同,情况并非如此。您可以轻松地更改为锯齿状阵列:

string[] MotorReplyArray = new string[30];
string[][] BayReplyArray = new string[][10];

现在BayReplyArray[i]是一个字符串数组,您可以在其上使用Array.Copy