简体   繁体   English

多维数组越界

[英]Multi dimensional array out of bounds

My code is hanging on the line fi_get[1, 4] with an "index out of bounds" error. 我的代码挂在fi_get[1, 4] ,出现“索引超出范围”错误。

The lines var is a string list array and at run time it had well over 600 lines of text in it. var lines是一个字符串列表数组,在运行时,其中有600多行文本。 I don't know why I can't assign a string value to [1,4] ? 我不知道为什么不能为[1,4]分配字符串值? It does assign values to [1,0] and [1,1] and [1,2] and [1,3] . 它确实为[1,0][1,1]以及[1,2][1,3]赋值。

    string[,] fi_get = new string[2, 11];

     if (lines[i].Contains("show fabric-interconnect firmware"))
            {
                fi_get[0, 0] = Regex.Replace(lines[i + 2], @".+Running-Kern-Vers:\s(.+)", "$1");
                fi_get[0, 1] = Regex.Replace(lines[i + 3], @".+Running-Sys-Vers:\s(.+)", "$1");
                fi_get[0, 2] = Regex.Replace(lines[i + 4], @".+Startup-Kern-Vers:\s(.+)", "$1");
                fi_get[0, 3] = Regex.Replace(lines[i + 5], @".+Startup-Sys-Vers:\s(.+)", "$1");
                fi_get[1, 0] = Regex.Replace(lines[i + 7], @".+Running-Kern-Vers:\s(.+)", "$1");
                fi_get[1, 1] = Regex.Replace(lines[i + 8], @".+Running-Sys-Vers:\s(.+)", "$1");
                fi_get[1, 2] = Regex.Replace(lines[i + 9], @".+Startup-Kern-Vers:\s(.+)", "$1");
                fi_get[1, 3] = Regex.Replace(lines[i + 10], @".+Startup-Sys-Vers:\s(.+)", "$1");
            }

            if (lines[i].Contains("show fabric-interconnect inventory expand detail"))
            {
                fi_get[0, 4] = Regex.Replace(lines[i + 2], @".+Product Name:\s(.+)", "$1");
                fi_get[0, 5] = Regex.Replace(lines[i + 3], @".+PID:\s(.+)", "$1");
                fi_get[0, 6] = Regex.Replace(lines[i + 4], @".+Serial (SN):\s(.+)", "$1");
                fi_get[0, 7] = Regex.Replace(lines[i + 7], @".+Description:\s(.+)", "$1");
                fi_get[0, 8] = Regex.Replace(lines[i + 10], @".+Serial (SN):\s(.+)", "$1");
                fi_get[0, 9] = Regex.Replace(lines[i + 13], @".+Product Name:\s(.+)", "$1");
                fi_get[0, 10] = Regex.Replace(lines[i + 14], @".+PID:\s(.+)", "$1");
                fi_get[0, 11] = Regex.Replace(lines[i + 15], @".+Serial (SN):\s(.+)", "$1");
                fi_get[1, 4] = Regex.Replace(lines[i + 29], @".+Product Name:\s(.+)", "$1");
                fi_get[1, 5] = Regex.Replace(lines[i + 30], @".+PID:\s(.+)", "$1");
                fi_get[1, 6] = Regex.Replace(lines[i + 31], @".+Serial (SN):\s(.+)", "$1");
                fi_get[1, 7] = Regex.Replace(lines[i + 34], @".+Description:\s(.+)", "$1");
                fi_get[1, 8] = Regex.Replace(lines[i + 37], @".+Serial (SN):\s(.+)", "$1");
                fi_get[1, 9] = Regex.Replace(lines[i + 40], @".+Product Name:\s(.+)", "$1");
                fi_get[1, 10] = Regex.Replace(lines[i + 41], @".+PID:\s(.+)", "$1");
                fi_get[1, 11] = Regex.Replace(lines[i + 42], @".+Serial (SN):\s(.+)", "$1");
            }

An error I see is on fi_get[0, 11] = Regex.Replace(lines[i + 15] because second index should be between 0 and 10!! 我看到的错误是fi_get[0, 11] = Regex.Replace(lines[i + 15]因为第二个索引应该在0到10之间!
Same error on fi_get[1, 11] = Regex.Replace(lines[i + 42] . fi_get[1, 11] = Regex.Replace(lines[i + 42]fi_get[1, 11] = Regex.Replace(lines[i + 42]相同的错误。

To answer your question 回答你的问题

why I can't assign a string value to [1,4]? 为什么不能将字符串值分配给[1,4]?

Remember that C# arrays are zero based, so if you define fi_get[1, 4] you can assign values from fi_get[0, 0] to fi_get [0, 3] . 请记住,C#数组基于零,因此,如果定义fi_get[1, 4] ,则可以将值从fi_get[0, 0]分配给fi_get [0, 3]
If you want to assign to fi_get[1, 4] you should define at least fi_get = new string[2, 5] . 如果要分配给fi_get[1, 4] ,则应至少定义fi_get = new string[2, 5]

I would bet that it's this expression: 我敢打赌,这是这样的表达:

lines[i + 29]

What is the value of i at the time this runs? 运行时i的值是多少?

i + 29

is probably out of range. 可能超出范围。 It's the only array index out of bounds exception that can be thrown on that line if this code is accurate. 如果此代码正确,则它是唯一可以在该行上引发的超出范围的数组索引异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM