在运行时动态创建数组并添加到另一个数组
我正在为我的下一款 iPhone 游戏开发一个小型等距引擎。为了存储地图单元格或图块,我需要一个二维数组。现在我用一维来伪造它,但它已经不够好了。
因此,从我在网络中发现的情况来看,在 Objective-C 中我需要创建一个数组的数组。 所以这是我的问题:如何根据我需要的地图行数在运行时动态创建数组?
第一个数组很简单:
NSMutableArray *OuterArray = [NSMutableArray arrayWithCapacity:mapSize];
现在我有了第一个数组,它应该包含所需的每一行的数组。 问题是,它可以是 10 个,也可以是 200 个甚至更多。所以我不想手动创建每个数组然后添加它。我认为必须有一种方法可以根据输入(例如所选的地图大小)在运行时创建所有这些数组。
希望你能帮助我 提前致谢 彼得
I am working on a small isometric engine for my next iPhone game. To store the map cells or tiles I need a 2 dimensionel array. Right now I am faking it with a 1-dimensionel, and it is not good enough anymore.
So from what I have found out looking around the net is that in objective-c I need to make an array of arrays.
So here is my question: How do I dynamicly create arrays at runtime based on how many map-rows I need?
The first array is easy enough:
NSMutableArray *OuterArray = [NSMutableArray arrayWithCapacity:mapSize];
now I have the first array that should contain an array for each row needed.
Problem is, it can be 10 but it can also be 200 or even more. So I dont want to manually create each array and then add it. I am thinking there must be a way to create all these arrays at runtime based on input, such as the chosen mapsize.
Hope you can help me
Thanks in advance
Peter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为前面的问题应该有所帮助。
目标c中的二维数组
与我无关。我从未拥有过 iPhone,也从未尝试过为 iPhone 编写代码。
I think this previous question should help.
2d arrays in objective c
Nothing to do with me. I have never owned an iphone or tried to write code for one.
NSMutableArray 的重点是你不关心。使用近似大小初始化两个维度,然后将其相加。如果您的阵列增长超出了您的初始估计,则后备存储将增加以容纳它。这对于您的列(一阶数组)和行(二阶数组)都有效。
编辑
不确定您在评论中的意思。但这是在 Objective-C 中动态创建二维可变数组的一种方法。
The whole point of NSMutableArray is that you don't care. Initialize both dimensions with an approximate size and then add to them. If your array grows beyond your initial estimate the backing storage will be increased to accomodate it. This counts for both your columns (first order array), and rows (second order array).
EDIT
Not sure what you meant in your comment. But this is one way to dynamically create a 2-dimensional mutable array in Objective-C.
NSMutableArray
可以容纳您想要添加的任意数量的元素。 (不过基于可用堆)。您所要做的就是当您想要向此可变数组添加元素(数组)时,您可以使用
addObject
方法添加它。因此,您创建一个 MutableArray,如下所示:
NSMutableArray
can hold as many elements as you want to add to it. (Based on available heap though).All you have to do is when you want to add an element(Array) to this mutable array you can add it using the
addObject
method.So you create a MutableArray as follows:
从您对其他答案的拒绝中,我认为您不知道如何将它们添加到循环中,或者我错了?
尝试:
或者,如果您知道或可以估计第二维的大小:
现在,如何填充数组,即从哪里获取内容取决于您。在一个或多个循环中,您执行以下操作:
From your rejection of the other answers I think you don't know how to add them in a loop, or am I wrong?
Try:
or, if you know or can estimate the size of the second dimension:
Now, how you fill the arrays, i.e. where you get the contents depends on you. In one or more loops, you do: