Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字符数组转换为二维整数数组失败_C#_Arrays_Multidimensional Array_Character - Fatal编程技术网

C# 字符数组转换为二维整数数组失败

C# 字符数组转换为二维整数数组失败,c#,arrays,multidimensional-array,character,C#,Arrays,Multidimensional Array,Character,我似乎很难将字符数组转换为二维整数数组。具体内容如下: 我想将包含特定分隔符的字符数组转换为多维数组,其中每一行的结尾由字符数组中的分隔符指定。我已经用不同的方法确保每一行具有相同的长度 该数组仅包含0和1 问题是,在测试该方法时,该方法似乎跳过了一行—读取第一行,下一行是0,读取第三行,第四行是0 以下是相关方法和测试方法: /// <summary> /// Converts a character array to 2D int array, where eac

我似乎很难将字符数组转换为二维整数数组。具体内容如下:

我想将包含特定分隔符的字符数组转换为多维数组,其中每一行的结尾由字符数组中的分隔符指定。我已经用不同的方法确保每一行具有相同的长度

该数组仅包含0和1

问题是,在测试该方法时,该方法似乎跳过了一行—读取第一行,下一行是0,读取第三行,第四行是0

以下是相关方法和测试方法:

    /// <summary>
    /// Converts a character array to 2D int array, where each character is a digit and only the digits of 1 and 0 are 
    /// allowed.
    /// </summary>
    /// <returns>The 2D int array.</returns>
    /// <param name="charArray">Character array.</param>
    /// <param name="delimiter">Delimiter.</param>
    public int[,] ConvertCharArrayTo2DIntArray(char[] charArray, char delimiter){
        int columnCounter = 0;
        //count how many rows
        while (charArray [columnCounter] != delimiter) {
            columnCounter++;
        }

        //count how many lines taking into account the delimiter
        int rows = charArray.Length/(columnCounter+1);

        int[,] twoDimArray = new int[rows, columnCounter];

        //count
        int h = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columnCounter; j++) {
                if (charArray [h] != '\n') {
                    twoDimArray [i, j] = (int)Char.GetNumericValue(charArray [h]);
                    //throw exception if the array contains numbers other than 1 or 0
                    if (twoDimArray [i, j] != 1 && twoDimArray [i, j] != 0)
                        throw new ArgumentException ();
                    h++;
                } else {
                    h++;
                    break;
                }
            }
        }
        return twoDimArray;
    }
    [Test()]
    public void TestConvertCharArrayTo2DIntArray(){
        HelperClass hc = new HelperClass ();
        int[,] twoDimArrayExpected = new int[,]{ 
            { 0, 1, 0, 1, 0 },
            { 0, 1, 0, 1, 0 },
            { 0, 1, 0, 1, 0 },
            { 0, 1, 0, 1, 0 } };
        char[] charArray = new char[] {'0','1','0','1','1','\n','1','1','1','1','1','\n','0','1','1','1','0','\n','0','1','0','1','0','\n'};
        int[,] twoDimArrayActual = hc.ConvertCharArrayTo2DIntArray (charArray, '\n');
        for (int i = 0; i < twoDimArrayExpected.GetLength (0); i++) {
            for (int j = 0; j < twoDimArrayExpected.GetLength (1); j++) {
                Console.Write (twoDimArrayActual [i,j]);
                //Commented out because it throws exceptions                                 
                //Assert.AreEqual(twoDimArrayExpected[i,j],
                //twoDimArrayActual[i,j]);
                if (j == twoDimArrayExpected.GetLength (1) - 1) {
                    Console.Write ("\n");
                }
            }
        }
    }
这是测试方法:

    /// <summary>
    /// Converts a character array to 2D int array, where each character is a digit and only the digits of 1 and 0 are 
    /// allowed.
    /// </summary>
    /// <returns>The 2D int array.</returns>
    /// <param name="charArray">Character array.</param>
    /// <param name="delimiter">Delimiter.</param>
    public int[,] ConvertCharArrayTo2DIntArray(char[] charArray, char delimiter){
        int columnCounter = 0;
        //count how many rows
        while (charArray [columnCounter] != delimiter) {
            columnCounter++;
        }

        //count how many lines taking into account the delimiter
        int rows = charArray.Length/(columnCounter+1);

        int[,] twoDimArray = new int[rows, columnCounter];

        //count
        int h = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columnCounter; j++) {
                if (charArray [h] != '\n') {
                    twoDimArray [i, j] = (int)Char.GetNumericValue(charArray [h]);
                    //throw exception if the array contains numbers other than 1 or 0
                    if (twoDimArray [i, j] != 1 && twoDimArray [i, j] != 0)
                        throw new ArgumentException ();
                    h++;
                } else {
                    h++;
                    break;
                }
            }
        }
        return twoDimArray;
    }
    [Test()]
    public void TestConvertCharArrayTo2DIntArray(){
        HelperClass hc = new HelperClass ();
        int[,] twoDimArrayExpected = new int[,]{ 
            { 0, 1, 0, 1, 0 },
            { 0, 1, 0, 1, 0 },
            { 0, 1, 0, 1, 0 },
            { 0, 1, 0, 1, 0 } };
        char[] charArray = new char[] {'0','1','0','1','1','\n','1','1','1','1','1','\n','0','1','1','1','0','\n','0','1','0','1','0','\n'};
        int[,] twoDimArrayActual = hc.ConvertCharArrayTo2DIntArray (charArray, '\n');
        for (int i = 0; i < twoDimArrayExpected.GetLength (0); i++) {
            for (int j = 0; j < twoDimArrayExpected.GetLength (1); j++) {
                Console.Write (twoDimArrayActual [i,j]);
                //Commented out because it throws exceptions                                 
                //Assert.AreEqual(twoDimArrayExpected[i,j],
                //twoDimArrayActual[i,j]);
                if (j == twoDimArrayExpected.GetLength (1) - 1) {
                    Console.Write ("\n");
                }
            }
        }
    }
输出如下:

01011

00000

11111

00000


我可能只是被卡住了,但现在我真的弄不明白这一点。

当内部循环结束时,您位于分隔符上。此时需要跳过递增控制变量h的字符分隔符

正如您现在所做的,您增加了控制变量,但随后中断了内部循环。然而,仅仅删除break指令是不起作用的,因为退出内部循环会增加外部循环的控制变量iof,使每个偶数行都为空且未处理

您可以尝试使用

int h = 0;
for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < columnCounter; j++)
    {
        twoDimArray[i, j] = (int)Char.GetNumericValue(charArray[h]);
        //throw exception if the array contains numbers other than 1 or 0
        if (twoDimArray[i, j] != 1 && twoDimArray[i, j] != 0)
            throw new ArgumentException();
        h++;
    }
    h++;
}

如果使用列表对象,则非常容易

 List<List<int>> twoDimArrayExpected = new List<List<int>>() { 
               new List<int>() { 0, 1, 0, 1, 0 },
               new List<int>(){ 0, 1, 0, 1, 0 },
               new List<int>(){ 0, 1, 0, 1, 0 },
               new List<int>(){ 0, 1, 0, 1, 0 } };

            string resutls = string.Join("\n", twoDimArrayExpected.Select(x => string.Join("", x)));​