C#中基于1的向量 [英] 1-Based Vectors in c#

查看:130
本文介绍了C#中基于1的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


代码:int [,] intMatrix2D1 =(int [,])Array.CreateInstance(typeof(int),new int [] {10,10},new int [] {1,1});能够生成2的下限为1的矩阵.但是,同一函数只能生成基于0的向量.我试图查找实现此目的的方法,而我得到的最接近的是Type.MakeArrayType方法页面上的一个模糊注释,内容为:

公共语言运行时在向量(即始终基于零的一维数组)和多维数组之间进行区分.一个始终只有一个维度的向量与恰好只有一个维度的多维数组不同.此方法重载只能用于创建向量类型,这是创建向量类型的唯一方法.使用MakeArrayType(Int32)方法重载来创建多维数组类型.

简而言之,是否有一种相当简单的方法来创建一个下限为1的一维数组?

谢谢
〜Scott

The code: int[,] intMatrix2D1 = (int[,])Array.CreateInstance(typeof(int), new int[] { 10, 10 }, new int[] { 1, 1 }); is able to make a 2d matrix with a lower bounds of 1. However, the same function can only make 0-based vectors. I''ve tried to look up ways to do this and the closest I''ve gotten is a cryptic note on the Type.MakeArrayType method page that states:

The common language runtime makes a distinction between vectors (that is, one-dimensional arrays that are always zero-based) and multidimensional arrays. A vector, which always has only one dimension, is not the same as a multidimensional array that happens to have only one dimension. This method overload can only be used to create vector types, and it is the only way to create a vector type. Use the MakeArrayType(Int32) method overload to create multidimensional array types.

Simply put, is there a fairly simple way to create a 1 dimensional array that has a lower bounds of 1?

Thank you,
~Scott

推荐答案

的一种相当简单的方法:System.Array.CreateInstance具有下限.即使数组排名大于1,也可以指定下限.请参见System.Array.CreateInstance(Type, Int32[], Int32[]).

如果这样做,则必须使用GetLowerBound, SetValue, GetValue等.请参见用法示例:
http://msdn.microsoft.com/en-us/library/system.array. getlowerbound.aspx [ ^ ].

以我的口味,这不值得打扰,但您应该自己决定.

如果您询问我的偏好设置,我将始终使用基于零的索引-在较低级别.

如果我需要实现任意索引基础,那么无论数组数据的等级如何,我都会在语义级别上创建数组数据的包装.在此包装器中,我将使用所需的索引类型创建索引属性("this").我将在属性getter/setter中偏移整数索引的值,以实现基于非零索引的效果.在内部,索引将保持从零开始.

您可以轻松地执行相同操作.同样,请自行决定.

—SA
This is a fairly simple way: System.Array.CreateInstance with lower bound. You can specify lower bounds even with the array rank more then 1, see System.Array.CreateInstance(Type, Int32[], Int32[]).

If you do this, you have to use GetLowerBound, SetValue, GetValue, etc. See the usage sample:
http://msdn.microsoft.com/en-us/library/system.array.getlowerbound.aspx[^].

To my taste, it does not worth bothering, but you should decide by yourself.

If you asked about my preferences, I would always work with zero-based indexing — at lower level.

If I had a requirement to implement arbitrary indexing bases, no matter what the rank of array data is, I would create a wrapper of the array data on semantic level. In this wrapper, I would create indexed property ("this") with required types of indices. I would offset the value of integer index in the property getter/setter to achieve the effect of non-zero-based indexing. Internally, indexing would remain zero-based.

You can easily do the same. Again, decide by yourself.

—SA


这篇关于C#中基于1的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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