具有整数的数组的数组

发布于 2024-12-11 17:48:28 字数 372 浏览 0 评论 0原文

您将如何将二维整数数组存储为类变量?

如果你想要一个整数数组,你可以:

类声明

int * myInts;

实现

int ints[3] = {1,2,3};
myInts = ints;

但是如果你想存储一个由整数组成的数组怎么办?

像这样:

 int ints[3][3] = {{1,2,3}, {1,2,3}, {1,2,3}};

我不想限制类声明中数组的大小,所以我想我必须使用指针,但是如何呢?

How would you go about storing a 2 dimensional array of ints as a class variable?

If you want an array of ints you go:

Class declaration

int * myInts;

Implementation

int ints[3] = {1,2,3};
myInts = ints;

But what if you want to store an array of arrays with ints?

Like this:

 int ints[3][3] = {{1,2,3}, {1,2,3}, {1,2,3}};

I don't wanna limit the size of the arrays in the class declaration so I guess I have to go with pointers, but how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

盛夏已如深秋| 2024-12-18 17:48:28

为了将来参考,这是我的结论:

类声明

 int ** ints;

实现

 int rows = 2;
 int cols = 5;

 ints = (int**)malloc(rows*sizeof(int*));
 ints[0] = (int*)malloc(cols*sizeof(int));

 ints[0][0] = 123;
 ints[0][1] = 456;
 ints[0][2] = 789;
 // etc

这是我自己对评论中提供的链接的解释,我的 C 技能相当低,所以请考虑到这一点;)也许有更好的方法可以一次输入多个数字,比如 {123,456,789} 之类的,但这超出了我现在的要求!

For future reference, this is my conclusion:

Class declaration

 int ** ints;

Implementation

 int rows = 2;
 int cols = 5;

 ints = (int**)malloc(rows*sizeof(int*));
 ints[0] = (int*)malloc(cols*sizeof(int));

 ints[0][0] = 123;
 ints[0][1] = 456;
 ints[0][2] = 789;
 // etc

This is my own interpretation of links provided in comments and my C skills are pretty low so take that into consideration ;) Maybe there are better ways to put in multiple numbers at a time with {123,456,789} or something, but that is beyond my requirements for now!

迷你仙 2024-12-18 17:48:28

我已经为你写了示例:

int N = 10, M = 15;
NSMutableArray *ints = [NSMutableArray arrayWithCapacity:N]; // array[N][M]
for (int i=0; i<N; i++)
{
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:M];
    for (int j=0; j<M; j++)
    {
        [arr addObject:[NSNumber numberWithInt:(i+1)*(j+1)]];
    }
    [ints addObject:arr];
}
// print
for (int i=0; i<[ints count]; i++)
{
    NSString *line = @"";
    NSMutableArray *arr = [ints objectAtIndex:i];
    for (int j=0; j<[arr count]; j++)
        line = [NSString stringWithFormat:@"%@ %@", line, [arr objectAtIndex:j]];
    NSLog(@"%@", line);
}

I've wrote sample for you:

int N = 10, M = 15;
NSMutableArray *ints = [NSMutableArray arrayWithCapacity:N]; // array[N][M]
for (int i=0; i<N; i++)
{
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:M];
    for (int j=0; j<M; j++)
    {
        [arr addObject:[NSNumber numberWithInt:(i+1)*(j+1)]];
    }
    [ints addObject:arr];
}
// print
for (int i=0; i<[ints count]; i++)
{
    NSString *line = @"";
    NSMutableArray *arr = [ints objectAtIndex:i];
    for (int j=0; j<[arr count]; j++)
        line = [NSString stringWithFormat:@"%@ %@", line, [arr objectAtIndex:j]];
    NSLog(@"%@", line);
}
苏佲洛 2024-12-18 17:48:28

如果要动态分配内存,换句话说,在运行时定义数组的大小,则需要将数组声明为指针,对其进行 malloc,然后在运行时向每个索引添加另一个 int 数组。您无法真正在类级别声明和动态分配。如果您使用 cocoa/iphone sdk,您可以使用 NSMutableArray。

您还可以创建自己的类来构造二维数组并公开推送和弹出 int 对象的方法,例如 [IntegerArray Push:x,y,n];

这是使用 的示例正如 Daniel R Hicks 指出的那样,双重引用

If you want to dynamically allocate memory, in other words define the size of the arrays at runtime, then you need to declare the array as a pointer, malloc it, and then add another array of ints to each index at runtime. You can't really declare and dynamically allocate at the class level. If you are using cocoa/iphone sdk you can use NSMutableArray.

You could also create your own class that constructs a two dimensional array and exposes methods to push and pop int objects like [IntegerArray push:x,y,n];

Here's and example of using a double reference as Daniel R Hicks pointed out.

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