声明引用数组。 [英] Declaring array of references.

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

问题描述

如何声明一个对

值数组元素的引用数组?


Ray

解决方案

要将值类型转换为引用类型,反之亦然使用

装箱/取消装箱。


int foo = 42;

object bar = foo; //拳击

int helloWorld =(int)bar; //取消装箱


在你的情况下:


int [] intArray = new int [...];

object [] boxedIntArray = new object [...];


for(int n = 0; n< intArray.Count; ++ n)

boxedIntArray [n] = intArray [n];


rayreeves schrieb:


如何声明数组对

值数组元素的引用?


Ray


Ray,


我认为最简单的方法是让引用数组将

索引存储到value数组中。我理解你的问题

了吗?


Brian


rayreeves写道:


如何声明一个对

值数组元素的引用数组?


Ray




rayreeves写道:


如何声明一个数组引用

值数组的元素?



你的意思是一个数组数组?你可以这样做:


int [] [] arrayOfArraysOfInts;


然后你必须说明你想要多少个数组数组:


arrayOfArraysOfInts = new int [15] [];


然后你必须分别创建每个值数组:


arrayOfArraysOfInts [0] = new int [12];

arrayOfArraysOfInts [1] = new int [23];

arrayOfArraysOfInts [ 2] = new int [52];

....


这就是你追求的目标吗?


How do I declare an array of references to the elements of an array of
values?

Ray

解决方案

To convert a value type to a reference type and vise versa use
boxing/unboxing.

int foo = 42;
object bar = foo; // boxing
int helloWorld = (int)bar; // unboxing

In your case:

int[] intArray = new int[...];
object[] boxedIntArray = new object[...];

for(int n = 0; n < intArray.Count; ++n)
boxedIntArray[n] = intArray[n];

rayreeves schrieb:

How do I declare an array of references to the elements of an array of
values?

Ray


Ray,

I think the easiest way is to have the reference array store the
indexes into the value array. Did I understand your question
correctly?

Brian

rayreeves wrote:

How do I declare an array of references to the elements of an array of
values?

Ray



rayreeves wrote:

How do I declare an array of references to the elements of an array of
values?

Do you mean an array of arrays? You can simply do this:

int[][] arrayOfArraysOfInts;

You then have to say how many arrays you want in the main array:

arrayOfArraysOfInts = new int[15][];

And you then have to create each array of values individually:

arrayOfArraysOfInts[0] = new int[12];
arrayOfArraysOfInts[1] = new int[23];
arrayOfArraysOfInts[2] = new int[52];
....

Is that what you were after?


这篇关于声明引用数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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