wpf嵌套的foreach索引数组超出范围

时间:2013-08-07 02:59:33

标签: c# arrays nested-loops

我创建了2个foreach循环,我收到错误(索引超出数组的范围),因为我使用了2个数组。

这就是我的所作所为:

using (StreamReader reader = File.OpenText(@"C:\Users\apr13mpsip\Documents\Visual Studio 2010\Projects\iStellarMobile\iStellarMobile\Puzzle\educational.txt"))
{
    string line;

    while ((line = reader.ReadLine()) != null) // read line by line.
    {
        column = line.Split(new string[] { "," }, StringSplitOptions.None);
        rowcol = column;
    }

    int  i = 0;
    int  j = 0;

    foreach (string r in rowcol)
    {
        foreach (string c in column)
        {
            var currentValue = rowcol[i][j];
            j++;
        }
        i++;                    
    }
}

我已经有了全局字符串[]列;和string [] rowcol;

此行发生错误:

  

var currentValue = rowcol [i] [j];

------- EDIT --------------------

这就是我的文本文件的样子:

enter image description here

根据Tim的回答,rowcol [0] [0]将打印h,而rowcol [0] [1]将打印E,但rowcol的第一个[]为null,只要大于0,它给我一个空错误,对象没有设置为引用。

enter image description here

1 个答案:

答案 0 :(得分:1)

我建议使用锯齿状数组,如果你遇到一行中有超过10列的情况,它就不会抛出出界错误。

我还建议使用File.ReadAllLines,因为它将返回文件中所有行的数组。

这样的事情(未经测试)应该让你前进:

string[] fileData = File.ReadAllLines(@"C:\Users\apr13mpsip\Documents\Visual Studio 2010\Projects\iStellarMobile\iStellarMobile\Puzzle\educational.txt");

string[] lineValues;

int row = 0;
int col;

string[][] rowcol = new string[fileData.Length][];

foreach (string line in fileData)
{
    lineValues = line.Split(new string[]{ ',' }, StringSplitOptions.None);

    rowcol[row] = new string[lineValues.Length];

    col = 0;

    foreach (string value in splits)
    {
        rowcol[row][col] = value;
        col++
    }

    row++;
}

此代码的作用如下:

1.  Populates the array `fileData` with the lines from the text file.
2.  Sets up an array to hold the split result on each line.
3.  Declares variables for row and col, and sets row to 0.
4.  Sets the first dimension of the jagged array `rowcol` to the length of the array of lines returned from `ReadAllLines` (i.e., the number of rows).
5.  Loop through each line in `fileData`, doing the following:
    a.  split the current line on ',' and put the results in `lineValues`.
    b.  Set the second array of `rowcol` for the current row equal to the length
        of the array `lineValues` (number of columns).
    c.  Set the `col` variable to 0 (first column).
    d.  Loop through each value in `lineValues`, assigning the current value to
        the proper column for the current row, and then increment the column counter.
6.  Once the inner loop is done, increment the row counter.

修改 rowcol是一个锯齿状数组(数组数组),这基本上意味着你有一个数组,其中每个元素是另一个数组引用。这些数组引用中的每一个都可以(但不必)具有不同的长度(在您的情况下它们不具有)。

string rowcol[][] = new string[fileData.Length][];

这会将锯齿状数组rowcol初始化为等于文件中行数的长度(10)。你现在在数组中有10个元素,但它们都是null。

rowcol[row] = new string[lineValues.Length];

这会将当前元素(行)的数组引用初始化为等于分割中值的长度(数字)。继续您的示例,您知道在元素“row”的数组中有“x”元素(来自拆分的值的数量),但它们是null。

rowcol[row][col] = value;

您知道有一个值可以访问的值(字符串)。

因此,如果您处于内部循环中,并且您拥有将打印rowcol[0][0]rowcol[1][0]值的代码,则在第一次通过内部循环后,您将获得{ {1}},但你不会在rowcol[0][0],因为你尚未排第1行,你仍然在第0行。

我在控制台应用中对此进行了测试,并使用以下代码段在外部循环完成后打印锯齿状数组的值:

rowcol[1][0]

这导致以下输出(整个输出未显示,只是足以给出一般的想法):

Row 0, Col 0: h
Row 0, Col 1: e
Row 0, Col 2: l
Row 0, Col 3: l
Row 0, Col 4: o
Row 0, Col 5:
Row 0, Col 6:
Row 0, Col 7:
Row 0, Col 8:
Row 0, Col 9:
Row 1, Col 0: o
Row 1, Col 1: 
Row 1, Col 2:
Row 1, Col 3:
Row 1, Col 4:
Row 1, Col 5:
Row 1, Col 6:
Row 1, Col 7:
Row 1, Col 8:
Row 1, Col 9:
Row 2, Col 0: l
Row 2, Col 1: i
Row 2, Col 2: v
Row 2, Col 3: e
Row 2, Col 4:
Row 2, Col 5:
Row 2, Col 6:
Row 2, Col 7:
Row 2, Col 8:
Row 2, Col 9:
....
Row 9, Col 0: 
Row 9, Col 1:
Row 9, Col 2:
Row 9, Col 3: d
Row 9, Col 4:
Row 9, Col 5:
Row 9, Col 6:
Row 9, Col 7:
Row 9, Col 8:
Row 9, Col 9: