C# 将字符串联接到2d数组,该数组已包含字符串或null
我想创建代码,将几个文本文件(每个文件都有不同的结构)连接到一个统一的构建中 现在,我设法打开所有文件,通过制表器将字符串拆分成行,并将所有内容保存到一个二维列表中C# 将字符串联接到2d数组,该数组已包含字符串或null,c#,arrays,string,list,C#,Arrays,String,List,我想创建代码,将几个文本文件(每个文件都有不同的结构)连接到一个统一的构建中 现在,我设法打开所有文件,通过制表器将字符串拆分成行,并将所有内容保存到一个二维列表中 Example of current data: 809187.49 226885.80 26934 809183.14 226877.21 26937a 2 5509514.58 6558911.86 0.00 80T 3 5509515.55 6558913.48 0.00 80T
Example of current data:
809187.49 226885.80 26934
809183.14 226877.21 26937a
2 5509514.58 6558911.86 0.00 80T
3 5509515.55 6558913.48 0.00 80T
4 5509516.35 6558914.56 0.00 80T
接下来,我将把二维列表中的内容重新打印到一个新数组中(有n行和2列),并同时对所有数据进行排序。所有双精度类型,大于100000.00,我想用“,”分隔符放入第二列,并将列表中每行的其余内容放入第一列:
Result:
26934 809187.49,226885.80
26937a 809183.14,226877.21
2,0.00,80T 5509514.58,6558911.86
3,0.00,80T 5509515.55,6558913.48
4,0.00,80T 5509516.35,6558914.56
这就是问题所在。我使用了如下代码:
string[,] resultArray = new string[RowNo, 2]; //Create Array
for (int i = 0; i < resultList.Count; i++)
{
for (int j = 0; j < Regex.Matches(file[i], " ").Count+1; j++)
{
if (double.TryParse(resultList[i][j], out n)) // record is a double type greater then n
{
if((Double.Parse(resultList[i][j]) % 1) > 0) // record is not a int type
{
if (resultArray[i, 1] != null) // if cell in array is not null
{
resultArray[i, 1].Insert(resultArray[i, 1].Count(), "," + resultList[i][j].ToString()); // add content of List in to string in second column
}
else // if cell in array is null for now
{
resultArray[i, 1].Insert(0, "," + resultList[i][j].ToString()); // put content of List in to second column
}
}
if (resultArray[i, 0] != null) //if cell in array is not null
{
resultArray[i, 0].Insert(resultArray[i, 0].Count(), ";" + resultList[i][j].ToString()); // put content of List in to first column
}
else // if cell in array is null for now
{
resultArray[i, 0].Insert(0, "," + resultList[i][j].ToString()); // put content of List in to first column
}
}
else
{
if (resultArray[i, 0] != null) //if cell in array is not null
{
resultArray[i, 0].Insert(resultArray[i, 0].Count(), ";" + resultList[i][j].ToString()); // put content of List in to first column
}
else // if cell in array is null for now
{
resultArray[i, 0].Insert(0, "," + resultList[i][j].ToString()); // put content of List in to first column
}
}
}
}
反而
resultArray[i, 1].Insert(resultArray[i, 1].Count(), "," + res_file[i][j].ToString());
但它不能正常工作。我收到了一些结果,但记录中的数据是重复的
我将感谢您的帮助。问题在于您的所有其他条件: 你在干什么
if (resultArray[i, 0] != null) //if cell in array is not null
{
resultArray[i, 0].Insert(resultArray[i, 0].Count(), ";" + resultList[i][j].ToString()); // put content of List in to first column
}
else // if cell in array is null for now
{
resultArray[i, 0].Insert(0, "," + resultList[i][j].ToString()); // put content of List in to first column
}
这基本上意味着当resultArray[i,0]!=null
,但在else块中,您试图访问该null对象的Insert()
方法。因此出现了错误。所有的else
块都是这样的
我想你要做的是在你的else块中做一个作业(你似乎已经弄明白了)。。。比如:
重复项的问题可能是您在中同时使用了+=
和左侧(LHS)表达式
resultaray[i,1]+=resultaray[i,1]+”,“+res_文件[i][j].ToString()代码>
当你做x+=1代码>与x=x+1相同代码>
你正在做:x+=x+1代码>与x=x+(x+1)相同代码>编译时,您会得到“对象引用未设置为对象的实例”吗?你跑的时候不行?抱歉弄错了。是的,你说得对。“对象引用未设置为对象的实例”在我运行时出现。是的,我了解是什么导致重复数据出现问题,这就是我决定放弃此解决方案的原因。但您知道如何解决填充仅包含空值的数组的问题吗?如果使用resultArray[I,0]+=“,“+resultList[I][j].ToString());,是否仍然会得到重复的数组;?不,很好。非常感谢:)
if (resultArray[i, 0] != null) //if cell in array is not null
{
resultArray[i, 0].Insert(resultArray[i, 0].Count(), ";" + resultList[i][j].ToString()); // put content of List in to first column
}
else // if cell in array is null for now
{
resultArray[i, 0].Insert(0, "," + resultList[i][j].ToString()); // put content of List in to first column
}
resultArray[i, 0] += "," + resultList[i][j].ToString());