C# Jagged Array 检查值是否存在/为真

标签 c# jagged-arrays

这是我的代码,我使用的是二维数组。

top = 0;
if(rowcol[i-1][j]){ top = rowcol[i-1][j] }

但是它显示了这个错误:

无法将类型“string”隐式转换为“bool”

完整代码:

 string[] fileData = File.ReadAllLines(@"C:\Users\apr13mpsip\Desktop\OneOrganizer\OneOrganizer\WordPuzzle\testing.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 lineValues)
            {
                rowcol[row][col] = value;
                col++;
            }
            row++;

        }


        for (int i = 0; i < rowcol.GetLength(0) ; i++)
        {
            for (int j = 0; j < rowcol[i].GetLength(0) ; j++)
            {


                int iadd =  i+1 <rowcol.GetLength(0) ? i + 1: 0;
                int iminus =   i-1> 0  ? i - 1 : 0;
                int jadd =  j+1 <rowcol.GetLength(0) ? j + 1 : 0;
                int jminus = j-1 > 0 ? j - 1 : 0;
                var self = rowcol[i][j]; // current value

                top = 0;
if(rowcol[iminus][j]){ top = rowcol[iminus][j] } // ERROR HERE
}
}

我实际上是从文本文件中读取数据,并通过在锯齿状数组中创建 10x10 行和列来从中创建内容。所以我可以通过 rowcol[][] 找到它们的值。

最佳答案

因为

if(rowcol[i-1][j])

将根据您的定义返回string

string[][] rowcol

if语句需要bool。你需要根据类似的东西检查它

if(rowcol[i-1][j] == "stringtotest")

关于C# Jagged Array 检查值是否存在/为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18205185/

相关文章:

c# - Nancy Self Host 使用 Razor View 的空白响应

c# - 在 C# 中动态存储矩阵值

c# - 不可调用成员 'File' 在生成报告时不能像方法一样使用

C# 通用接口(interface)和工厂模式

arrays - 如何在Unity3d检查器中显示锯齿状数组?

javascript - 使用lodash比较锯齿状数组(项目无序存在)

arrays - 交错数组作为 vba sub 的参数

c# - 如何在不迭代的情况下在包装时移动对象数组?

c# - 是否有替代 IRibbonControl.Context 的解决方法或替代方法来访问 Excel 2016 或更高版本中的正确窗口和工作簿?

c# - 从 C# 调用 DLL 时,为什么小结构会错位函数参数?