引用2D数组元素 [英] Referencing 2D Array elements

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

问题描述

大家好,



我可能在这里做了一些非常明显的错误,但我不知道如何修改我的代码来纠正它。只是想知道是否有人有任何建议:



我创建变量:

数组[,] myLevel = {{Green,Brown,positions1},{Green2,Brown2,positions2}}; 



其中每个元素Green,Brown,positions1等属于输入int [,]。



然后我想创建我方法的实例游戏:

  public 游戏( int  [,] Grass, int  [,] Rocks, int  [,] position, int 级别)





我使用以下行尝试调用方法:

游戏game1 = < span class =code-keyword> new 游戏(myLevel [thislevel, 0 ],myLevel [thislevel, 1 ],myLevel [thislevel, 2 ],thislevel); 





我收到错误消息无法从System.Array转换为int [*,*]



非常感谢任何建议。



Stephen。

解决方案

< blockquote>你需要将myLevel声明为

  int  [,] [,] myLevel 


myLevel 是多维 int 数组的多维数组,即:

  int  [,] [,] myLevel = {{Green,Brown,positions1} ,{Green2,Brown2,positions2}}; 


感谢您的所有建议。 Tomas你的建议有效,并感谢Fredrick的替代解决方案。另外,感谢清除我的混乱CPallini。这些提示有很大帮助。



Stephen。


Hi all,

I'm probably doing something very obviously wrong here but I'm not sure how to modify my code to correct it. Just wondering if anyone has any advice:

I create the variable:

Array[,] myLevel = { { Green, Brown, positions1 }, { Green2, Brown2, positions2 } };


where each of the elements Green, Brown, positions1, etc. are of the type int[,].

I then want to create an instance of my method Game:

public Game(int[,] Grass, int[,] Rocks, int[,] positions, int level)



I have used the following line to try to call the method:

Game game1 = new Game(myLevel[thislevel, 0], myLevel[thislevel, 1], myLevel[thislevel, 2], thislevel);



I get the error message "Cannot convert from System.Array to int[*,*].

Thanks very much for any advice.

Stephen.

解决方案

You need to declare myLevel as

int[,][,] myLevel


myLevel is a multidimensional array of multidimensional int arrays, that is:

int[,] [,] myLevel ={ { Green, Brown, positions1 }, { Green2, Brown2, positions2 } };


Thanks for all of your advice. Tomas your suggestion worked, and thanks for the alternative solution also Fredrick. Also, thanks for clearing up my confusion CPallini. These tips have helped greatly.

Stephen.


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

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