在C#中调整多维数组的大小 [英] Resizing multidimensional array in C#

查看:357
本文介绍了在C#中调整多维数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在C#的控制台中调整多维数组的大小。

I want to know how I can resize multidimensional array in the console of the C#.

推荐答案





你无法真正调整阵列的大小;但是你可以根据原来的数据重新调整数组,正如Espen在评论中正确解释的那样。



调整数组的大小是可能的,但不推荐,因为这不是有效,因为整个数组被复制;数组并非专为此类设计。



[结束编辑]



请参阅:



http://msdn.microsoft.com/en -us / library / bb348051.aspx [ ^ ]。



您最好使用更灵活的解决方案 System.Collections.Generic 。例如,以下声明将代表锯齿状数组,其中 rank 3:



You cannot really resize the array; but you can have resized array based on the original one, as Espen correctly explained in his comment.

Resizing an array is possible, but not recommended, because this is not effective, as the whole array is copied; arrays are not designed for such things.

[END EDIT]

Please see:

http://msdn.microsoft.com/en-us/library/bb348051.aspx[^].

You should better use more flexible solution with System.Collections.Generic. For example, the following declaration will represent a
jagged array with the rank 3:
using System.Collections.Generic;

//...
List<List<List<int>>> list;





请参阅:

http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx [ ^ ]。



您可以随时添加任意数字元素,但您应该记住数组是锯齿状的,也就是说,每个元素都可以有不同的长度。如果要表示矩形多维矩阵,则需要注意在每个维度中同步添加相同数量的元素。为了解决这个问题,我通常建议如下:将列表封装在一个实现类似数组的接口的类或结构中,并自动保持数组均匀(矩形)。为此,您可以使用C#索引属性,其语法与数组相同:



Please see:
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^],
http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx[^].

You can add any number element any time, but you should remember that the "array" is jagged, that is, every element can have different length. If you want to represent a "rectangular" multidimensional matrix, you need to take care about adding equal number of elements in each dimension in sync. To solve this problem, I usually recommend the following: encapsulate your list in a class or structure which implements an array-like interface and automatically keeps the "array" even ("rectangular"). For this purpose, you can use C# indexed properties with the syntax identical to the one of arrays:

class ArrayIfOfDouble3D {

    internal // or public
    double this[int x, int y, int z] {
        get { /* ... */ } // all the trick is the implementation of the getter
        set { /* ... */ } // ...and the setter
    } //this

    List<List<List<int>>> list = new List<List<List<int>>>();

    // ...

}





在实现中,您应该保持每个维度的数组的当前大小。当用户写入超出当前大小的某个矩阵单元格时,实现应该添加具有一些默认值的元素(一个很好的候选者 double.NaN !)并在所需的文件中填写单元格(仅一个),具有用户提供的值。如果您尝试读取超出当前大小的值,则可以返回默认值。



-SA



In the implementation, you should keep the current size of the "array" along each dimension. When a user writes to some matrix cell beyond current sizes, the implementation should add the elements with some default value (an excellent candidate it double.NaN!) and fiil in the required cell (only one) with the value supplied by the user. If you try to read the value beyond the current size, you can just return the default value.

—SA


你不能。 Array''一旦创建,就无法调整大小。



你可以做的是创建一个具有所需大小的新数组并从旧数组中复制数据到新的。
You can''t. Array''s, once created, cannot be resized.

What you CAN do, is create a new array with the required size and copy the data from the old array to the new one.


这篇关于在C#中调整多维数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆