【问题标题】:Bring multiple 2d arrays [][] on same length将多个二维数组 [][] 放在相同的长度上
【发布时间】:2017-08-04 10:52:21
【问题描述】:

在处理锯齿状数组 [][] 时,我遇到了一个大问题。

我编写了一个与大量 CSV 文件交互的程序。它会读取它们然后比较它们。现在我有一个问题,如果数组 A 具有 10 行和 10 列的维度,但数组 B 只有 5 行和 5 列的维度。我在数组 B 上得到“超出范围”。这只是一个例子,如果我有一个数组,每列中的行数不同...

我尝试检查“null”,但这不起作用,因为一旦它尝试访问该字段,我就会得到“超出范围”......

现在我有两个理论来解决这个问题:

A.)检查数组 B 中的“超出范围”,如果是,则在同一字段中用“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
             }
        }
    }
}

【问题讨论】:

  • 数组有方法 GetLength() 检查一下,可能会有帮助。
  • 是的,但我需要每个字段的 GetLength,我不知道如何为锯齿状数组完成此操作 =/

标签: c# arrays multidimensional-array jagged-arrays multi-dimensional-scaling


【解决方案1】:

您可以在for 循环中检查两个数组的Lengths:

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                           
        }
    }
}

【讨论】:

  • 好吧,这还不算太糟糕,我不会再超出范围了,但是我会跳过很多字段 =/ 我宁愿希望最大的数组支配循环,如果较小的数组超出范围,那么例如添加一个 0 或任何东西。你知道我的意思?感谢您的时间和精力!
  • @christian890,种子编辑的答案,如果数组有足够的元素(在if内),您可以执行一些操作,或者当某个数组中没有元素(在else内)时执行另一个操作
  • @christian890,见编辑-更改嵌套for循环-for (int b = 0; b &lt; Math.Max(array1[a].Length, array2[a].Length); b++)
  • @christian890,让我们continue this discussion in chat
  • @christian890 请阅读:about follow-up questions
猜你喜欢
  • 1970-01-01
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 2014-04-23
  • 2013-08-21
  • 2023-03-05
  • 1970-01-01
  • 2014-12-20
相关资源
最近更新 更多