在运行时动态创建数组并添加到另一个数组

发布于 2024-11-28 05:24:32 字数 432 浏览 2 评论 0原文

我正在为我的下一款 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

游魂 2024-12-05 05:24:32

我认为前面的问题应该有所帮助。

目标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.

以歌曲疗慰 2024-12-05 05:24:32

NSMutableArray 的重点是你不关心。使用近似大小初始化两个维度,然后将其相加。如果您的阵列增长超出了您的初始估计,则后备存储将增加以容纳它。这对于您的列(一阶数组)和行(二阶数组)都有效。


编辑

不确定您在评论中的意思。但这是在 Objective-C 中动态创建二维可变数组的一种方法。

NSUInteger columns = 25 ; // or some random, runtime number
NSUInteger rows = 50;     // once again, some random, runtime number

NSMutableArray * outer = [NSMutableArray arrayWithCapacity: rows];

for(NSUInteger i; i < rows; i++) {
    NSMutableArray * inner = [NSMutableArray arrayWithCapacity: columns];
    [outer addObject: inner];
}

// Do something with outer array here

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.

NSUInteger columns = 25 ; // or some random, runtime number
NSUInteger rows = 50;     // once again, some random, runtime number

NSMutableArray * outer = [NSMutableArray arrayWithCapacity: rows];

for(NSUInteger i; i < rows; i++) {
    NSMutableArray * inner = [NSMutableArray arrayWithCapacity: columns];
    [outer addObject: inner];
}

// Do something with outer array here
少女情怀诗 2024-12-05 05:24:32

NSMutableArray 可以容纳您想要添加的任意数量的元素。 (不过基于可用堆)。

您所要做的就是当您想要向此可变数组添加元素(数组)时,您可以使用 addObject 方法添加它。

因此,您创建一个 MutableArray,如下所示:

NSMutabaleArray *outerArray = [NSMutableArray array]; // initially contains 0 elements.

[outerArray addobject:<anotherArray>];

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:

NSMutabaleArray *outerArray = [NSMutableArray array]; // initially contains 0 elements.

[outerArray addobject:<anotherArray>];
奶气 2024-12-05 05:24:32

从您对其他答案的拒绝中,我认为您不知道如何将它们添加到循环中,或者我错了?

尝试:

for (i = 0; i < mapSize; i++) 
    [outerArray addObject: [[NSMutableArray alloc] init]];

或者,如果您知道或可以估计第二维的大小:

for (i = 0; i < mapSize; i++) 
    [outerArray addObject: [[NSMutableArray alloc] 
                            initWithCapacity: your_2nd_d_size]];

现在,如何填充数组,即从哪里获取内容取决于您。在一个或多个循环中,您执行以下操作:

[(NSMutableArray *)[outerArray objectAtIndex: i] 
    addObject: your_current_object]; 

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:

for (i = 0; i < mapSize; i++) 
    [outerArray addObject: [[NSMutableArray alloc] init]];

or, if you know or can estimate the size of the second dimension:

for (i = 0; i < mapSize; i++) 
    [outerArray addObject: [[NSMutableArray alloc] 
                            initWithCapacity: your_2nd_d_size]];

Now, how you fill the arrays, i.e. where you get the contents depends on you. In one or more loops, you do:

[(NSMutableArray *)[outerArray objectAtIndex: i] 
    addObject: your_current_object]; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文