将多个二维数组 [][] 放在相同的长度上
Bring multiple 2d arrays [][] on same length
我在处理锯齿状数组时遇到了一个大问题 [][].
我编写了一个与大量 CSV 文件交互的程序。它将读取它们然后比较它们。如果数组 A 的维度为 10 行和 10 列,但数组 B 的维度仅为 5 行和 5 列,现在我遇到了一个问题。我在数组 B 上得到 "out of range"。这只是一个例子,如果我有一个数组,每列中的行数不同,情况会更糟...
我尝试检查 "null" 但这不起作用,因为一旦它尝试访问该字段我就得到了 "out of range"...
现在我有两个理论可以解决这个问题:
A.)检查数组 B 中的 "out of range",如果是,则在同一字段中用“0”
填充数组 A
B.) 检查数组 A 和数组 B 是否具有相同的维度,如果不相同,则用“0”填充数组,使其具有相同的数量
对于这两种解决方案,我完全不知道如何在 C# 中执行此操作...我总是超出范围...
我目前对 1 个阵列所做的是:
for (int b = CSV_Statistiken.Length - 1; b >= 0; b--)
{
for (int a = 0; a < CSV_Statistiken[b].Length; a++)
{
CSV_Statistiken[b][a] = 1;
}
}
所以我得到数组的维度并遍历它,将每个值设置为 1。但是我该如何处理我的 2 个数组问题?
我研究了一下,但找不到任何解决方案 =/
提前致谢
编辑:例如我正在尝试做的事情:
for (int i = 0; i < number; i++) //runs through every File existing
{
NextFile = fold.Filepath + "\" + files[i].ToString();
file = new FileInfo(@NextFile);
max_Rows = 0;
max_Col = 0;
CSV_temp = ReadCSV(file, ref max_Rows, ref max_Col); // reads the next file to an arraay [][] and saves the size of this array in max_col/ max_rows
MAX_Col_Total = GetHighestValues(ref MAX_Col_Total, max_Col);
MAX_Rows_Total = GetHighestValues(ref MAX_Rows_Total, max_Rows);
for (int j = 0; j < MAX_Col_Total; j++) //runs thrugh the max amount of cols found
{
for (int k = MAX_Rows_Total - 1; k >= 0; k--) //runs through the max mount of rows found
{
if (CSV_temp.GetLength(0) >= j && CSV_temp.GetLength(1) >= k)//Checks if Field exists -> does NOT work!
{
if (CSV_temp[k][j] > (Threshhold))) //
{
do something
}
}
else
{
// Field doesnt exists -> do something else
}
}
}
}
您可以在 for
循环中检查两个数组的 Length
s:
for (int a = 0; a < array1.Length && a < array2.Length; a++)
{
for (int b = 0; b < array1[a].Length && b < array2[a].Length; b++)
{
//compare
}
}
现在你的循环永远不会超出任何数组索引,你不会得到 IndexOutOfRangeException
。
编辑:
var biggestLength1 = Math.Max(array1.Length, array2.Length);
for (int a = 0; a < biggestLength1; a++)
{
var biggestLength2 = 0;
if (array1.Length > a && array2.Length > a)
{
biggestLength2 = Math.Max(array1[a].Length, array2[a].Length);
}
else
{
biggestLength2 = array1.Length > a ? array1.Length : array2.Length;
}
for (int b = 0; b < biggestLength2; b++)
{
if (a < array1.Length &&
a < array2.Length &&
b < array1[a].Length &&
b < array2[a].Length)
{
// every array has enough elements count
// you can do operations with both arrays
}
else
{
// some array is bigger
}
}
}
我在处理锯齿状数组时遇到了一个大问题 [][].
我编写了一个与大量 CSV 文件交互的程序。它将读取它们然后比较它们。如果数组 A 的维度为 10 行和 10 列,但数组 B 的维度仅为 5 行和 5 列,现在我遇到了一个问题。我在数组 B 上得到 "out of range"。这只是一个例子,如果我有一个数组,每列中的行数不同,情况会更糟...
我尝试检查 "null" 但这不起作用,因为一旦它尝试访问该字段我就得到了 "out of range"...
现在我有两个理论可以解决这个问题:
A.)检查数组 B 中的 "out of range",如果是,则在同一字段中用“0”
填充数组 AB.) 检查数组 A 和数组 B 是否具有相同的维度,如果不相同,则用“0”填充数组,使其具有相同的数量
对于这两种解决方案,我完全不知道如何在 C# 中执行此操作...我总是超出范围...
我目前对 1 个阵列所做的是:
for (int b = CSV_Statistiken.Length - 1; b >= 0; b--)
{
for (int a = 0; a < CSV_Statistiken[b].Length; a++)
{
CSV_Statistiken[b][a] = 1;
}
}
所以我得到数组的维度并遍历它,将每个值设置为 1。但是我该如何处理我的 2 个数组问题?
我研究了一下,但找不到任何解决方案 =/
提前致谢
编辑:例如我正在尝试做的事情:
for (int i = 0; i < number; i++) //runs through every File existing
{
NextFile = fold.Filepath + "\" + files[i].ToString();
file = new FileInfo(@NextFile);
max_Rows = 0;
max_Col = 0;
CSV_temp = ReadCSV(file, ref max_Rows, ref max_Col); // reads the next file to an arraay [][] and saves the size of this array in max_col/ max_rows
MAX_Col_Total = GetHighestValues(ref MAX_Col_Total, max_Col);
MAX_Rows_Total = GetHighestValues(ref MAX_Rows_Total, max_Rows);
for (int j = 0; j < MAX_Col_Total; j++) //runs thrugh the max amount of cols found
{
for (int k = MAX_Rows_Total - 1; k >= 0; k--) //runs through the max mount of rows found
{
if (CSV_temp.GetLength(0) >= j && CSV_temp.GetLength(1) >= k)//Checks if Field exists -> does NOT work!
{
if (CSV_temp[k][j] > (Threshhold))) //
{
do something
}
}
else
{
// Field doesnt exists -> do something else
}
}
}
}
您可以在 for
循环中检查两个数组的 Length
s:
for (int a = 0; a < array1.Length && a < array2.Length; a++)
{
for (int b = 0; b < array1[a].Length && b < array2[a].Length; b++)
{
//compare
}
}
现在你的循环永远不会超出任何数组索引,你不会得到 IndexOutOfRangeException
。
编辑:
var biggestLength1 = Math.Max(array1.Length, array2.Length);
for (int a = 0; a < biggestLength1; a++)
{
var biggestLength2 = 0;
if (array1.Length > a && array2.Length > a)
{
biggestLength2 = Math.Max(array1[a].Length, array2[a].Length);
}
else
{
biggestLength2 = array1.Length > a ? array1.Length : array2.Length;
}
for (int b = 0; b < biggestLength2; b++)
{
if (a < array1.Length &&
a < array2.Length &&
b < array1[a].Length &&
b < array2[a].Length)
{
// every array has enough elements count
// you can do operations with both arrays
}
else
{
// some array is bigger
}
}
}