【问题标题】:Adding a 2D array to a Container class through its method .Add通过 Container 类的方法 .Add 将 2D 数组添加到
【发布时间】:2019-03-05 14:54:13
【问题描述】:

我无法将我的二维数组添加到我的容器类中。帮助将不胜感激

这是我的容器类:

 class Container
{
    string[,] Matrix;
    public int Rows { get; private set; }
    public int Columns { get; private set; }


    public Container(int rows, int columns)
    {
        Rows = rows;
        Columns = columns;
        Matrix = new string[Rows, Columns];
    }

    public void Add(string[,] s1, int height, int width)
    {
        Matrix[Rows++, Columns++] = s1[height, width];
    }

    public string Take(int height, int width)
    {
        return Matrix[height, width];
    }

错误:

未处理的异常:System.IndexOutOfRangeException:索引超出了数组的范围。

在 C:\Users\Justas\Desktop\Bandymas\ConsoleApp1\Program.cs:line 26 中的 ConsoleApp1.Container.Add(String[,] s1, Int32 height, Int32 width) 在 C:\Users\Justas\Desktop\Bandymas\ConsoleApp1\Program.cs:line 92 中的 ConsoleApp1.Program.Read(Int32 n, Container Matrix) 在 C:\Users\Justas\Desktop\Bandymas\ConsoleApp1\Program.cs:line 45 中的 ConsoleApp1.Program.Main(String[] args) 按任意键继续 。 . .

当我尝试使用我的容器类方法 .Add 添加我的二维数组时发生错误:

        Matrix.Add(array, rowLength, colLength);

这是二维数组的样子:

string[,] array = new string[n, n];

        var list = Enumerable
        .Range(0, file.Length / n)
        .Select(i => file.Substring(i * n, n))
        .ToList();

        var res = string.Join(Environment.NewLine, list);
        for (int i = 0; i < n; i++)
        {
            char[] row = list[i].ToCharArray();
            for (int j = 0; j < n; j++)
            {
                array[i, j] = row[j].ToString();
            }
        }

        int rowLength = array.GetLength(0);
        int colLength = array.GetLength(1);

这就是我的二维数组的样子:

    Berzas,su
    la;;sula;
    ;klevu sa
    ldial lap
    asula  a 
      aula, a
    r  suart 
    zemes vai
    kai du   

这是一个 9x9 数组,我正在尝试将其添加到我的容器中

非常感谢您的帮助

【问题讨论】:

  • 您对Add() 的调用会增加行数和高度,并尝试访问它。根据定义,它将超出数组原始构造设置的边界。 C#/.net 中的Arrays 大小固定。
  • @willaien 我明白了,我将如何解决这个问题?因为我必须使用容器类
  • 使用类似于 List&lt;T&gt; 内部使用的加倍算法:跟踪表观大小(行数、高度)和实际大小(实际行数、实际高度),然后在 Add() 内部方法,每次添加操作溢出时使用Array.Resize()将底层数组的大小加倍(确保跟踪新的实际大小)

标签: c# arrays multidimensional-array containers


【解决方案1】:

如果我理解正确,我不确定,你要做的是通过在原始矩阵的对角线中添加一个元素来增加二维数组:

例如:

row = 2
column = 2


    Matrix = |'str1' | 'str2'|     s = |'str10' | 'str11' |
             |'str3' | 'str4'|         | ...    | ...    |

这样当Container.Add(s,0,0)被调用时,预期的结果就是有

Matrix = |'str1' | 'str2'| empty |    
         |'str3' | 'str4'| empty |
         |empty  | empty | 'str10'|

这就是我对您的代码的理解(也许我错了)。然后,出现错误是因为您在 Matrix 数组为 2x2 时尝试访问 Matrix[2,2],因此没有第三列。一种解决方案是

public void Add(string[,] s1, int height, int width)
{
    # Create a new Array
    var newMatrix = new string[Rows++,Columns++]

    # Pass the old array to the new one
    for(i=0;i<Rows-1;i++)
    {
        for(j=0;j<Columns-1;j++)
        {
           newMatrix[i,j] = Matrix[i,j];
        }
    }

    # Add the new element
    newMatrix[Rows, Columns] = s1[height, width];

    # Then make the new matrix the good one
    Matrix = newMatrix; 
}

我希望这很有用,如果我错了,我会确保更改它。

编辑:

好的,现在我想我明白了,您想要的是将数组存储到容器类中,而不是添加元素。所以你应该做的是,

public void Add(string[,] s1, int height, int width)
{
    Matrix  = s1;
    Rows = height;
    Columns = width;
}

也许我过于简单化了。告诉我

【讨论】:

  • 感谢您的回复。我尝试了你的方法,但我仍然得到错误索引超出范围。我已经用我的二维数组的样子更新了我的帖子,我不知道这样做是否会对回答问题的人有很大帮助,但老实说我没有想法。我想要做的是将array 中的元素添加到我的Container 类属性Matrix,我不想增加大小,只是将元素从我的array 转移到我的@ 987654330@
  • @EncoreWLT 所以你不想在数组的对角线上添加一个元素?您想要的是,假设您在容器中有一个 9x9 矩阵,在添加数组后使其变为 18x9?编辑:我明白了,然后让我修改答案
  • 我不想让数组变大。我只想将数组中的元素转移到位于集合类中的 Matrix 数组中。矩阵数组也必须是 9x9
  • 感谢您的回复。我会将您的答案标记为正确,因为我不再有错误。但是,我将如何使用 Container 类中的 .Take 方法来使用数组中的元素
  • @EncoreWLT 没问题,只要 height
猜你喜欢
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 2013-02-24
  • 2015-03-10
  • 2021-06-07
  • 1970-01-01
  • 2014-08-13
相关资源
最近更新 更多