数组越界?

8bi*_*cat 0 c# arrays

我一直在让数组索引超出范围?

我试过改变mymatches.Count到+1和-1但它仍然超出范围.

为什么?

   public string[] readAllScripts()
    {
        string[] scripts = txt_script.Lines;

        int arraysize = scripts.Length;
        int x = 0;
        int y = 0;
        MessageBox.Show(arraysize.ToString());

        //string pattern = "[a-zA-Z]*";
        string[][] scriptarray = new string[arraysize][];

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

            MatchCollection mymatches = Regex.Matches(scripts[i], "[a-zA-Z]*");

            scriptarray[i] = new string[mymatches.Count];

            foreach (Match thematch in mymatches)
            {
                scriptarray[x][y] = thematch.Value;
                y++;
            }
            x++;
        }



        return scripts;
    }
Run Code Online (Sandbox Code Playgroud)

Kri*_*ten 5

看起来你需要在循环中重新初始化y:

public string[] readAllScripts() 
{ 
    string[] scripts = txt_script.Lines; 

    int arraysize = scripts.Length; 
    int x = 0; 

    MessageBox.Show(arraysize.ToString()); 

    //string pattern = "[a-zA-Z]*"; 
    string[][] scriptarray = new string[arraysize][]; 

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

        MatchCollection mymatches = Regex.Matches(scripts[i], "[a-zA-Z]*"); 

        scriptarray[i] = new string[mymatches.Count]; 

        int y = 0; 
        foreach (Match thematch in mymatches) 
        { 
            scriptarray[x][y] = thematch.Value; 
            y++; 
        } 
        x++; 
    } 

    return scripts; 
} 
Run Code Online (Sandbox Code Playgroud)