将数组传递给方法 [英] passing Arrays to methods

查看:80
本文介绍了将数组传递给方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
通过使一维不变,可以将3维数组传递给以2维数组作为参数的方法.如果可能的话,传递的参数(3-D数组)应如何显示?

例如3-D数组是Array_Name [i,j,k],而我想将Array_Name [i,j,2]传递给方法.

hello every one
is it possible to pass a 3-dimension array to a method that has a 2-dimension array as an argument by making one dimension constant. if possible, how should the passing argument (3-D array) looks like?

for example 3-D array is Array_Name[i,j,k] and i want to pass Array_Name[i,j,2] to the method.

推荐答案

示例未传递数组,而是传递了数组元素,这与传递与数组元素相同类型的变量相同.

但是,您也可以轻松地对数组进行路径设置.数组是引用类型,因此您只需要按值传递数组引用本身即可.

Your example is not passing the array, this is passing the array element, which is the same as passing a variable of the same type as the array element.

However, you can path an array as well, with no hassles. The arrays are reference types, so you simply need to pass the array reference itself by value.

double[,,] doubleArray = new double[4,8,12]; // 3D array of a primitive type initialized as 4x8x12 array

struct ReallyBigStructure {/*...*/} // just to demonstrate you can work with them in arrays too; value type
class SomeElement {/*...*/} // reference type

ReallyBigStructurep[,,] arrayOfStructures = new ReallyBigStructurep[10,20,3];
SomeElement[,,] arrayOfClassInstances = new SomeElement[11,21,31];

//...

// pass all the arrays by value, as they are reference types:
void WorkWithDoubleArray(double[,,] array) {/* ... */} 
void WorkWithStructureArray(ReallyBigStructure[,,] array) {/* ... */}
void WorkWittClassInstanceArray(SomeElement[,,] array) {/* ... */}

// and the calls would be:
void WorkWithDoubleArray(doubleArray);
void WorkWithStructureArray(arrayOfStructures);
void WorkWittClassInstanceArray(arrayOfClassInstances);

// if you need to pass array element, it's no different from passing of a parameter of element type;
void PassAClassInstance(SomeElement value); //never need ref or out
void PassDoubleByValue(double value);
void PassDoubleReference(ref double value); //can input and return value, but it makes no sense as you use return
void OutpuDoubleByReference(out double value); //output-only value, but it makes no sense as you use return param

// but it would make sense as otherwise you would copy it all on stack:
void PassBigStructureByReference(ref ReallyBigStructure);
// out makes sense, too, but by-value passing may have a problem of too copying too much data on stack;
// not a problem for smaller structured



—SA



—SA


如果按照SAKryukov的说明,您只想定义一个常量3rd索引器,则您的方法应该只将3个整数用作索引器值,并且可以传递你的女third,第三个,

但是,实际上您在谈论锯齿状数组(对于3D锯齿状数组,是值数组的数组),您可以尝试对数组进行扩展方法,类似

if, as SAKryukov states, you simply want to define a constantant 3rd indexer then your method should just take 3 ints for indexer values and you can pass your sonstant to the third,

However, on the offchance that you are actually talking about jagged arrays (an array of arrays of arrays of values, for a 3D jagged array) you could try an extension method on array, something along the lines of

public static T[][][] AddConstantDimension<t>(this T[][] array, int extraDimensionLength, T extraVal)
        {
            T[][][] result = new T[][][] {};
            
            for (int i = 0; i < array.GetLength(0); i++)
            {
                T[][] inner = new T[][] { };
                
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    T[] inner2 = new[] {extraVal};
                    inner[j] = inner2;
                }

                result[i] = inner;
            }

            return result;
        }

</t>



那么您对方法的调用将是



Then your call to your method would be

someObject.Method(2dJaggedArray.AddConstantDimension(5,2))



这将添加长度为5的第3维,并插入值2

注意:以上内容均未经测试,但至少应提供一个起点.



which would add a 3rd dimension, of length 5, with the value 2 inserted

NB: none of the above is tested, but should provide at least a starting point


尝试.NET Framework中的元组.参考: http://msdn.microsoft.com/en-us/library/system.tuple .aspx [^ ]
Try Tuples in .NET Framework. Ref at: http://msdn.microsoft.com/en-us/library/system.tuple.aspx[^]


这篇关于将数组传递给方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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