将 int 与 int 数组进行比较 Obj-C

发布于 2024-08-28 07:57:52 字数 94 浏览 4 评论 0 原文

我如何查看我的整数是否在整数数组中...

例如我想知道 7 是否在 [ 1 3 4 5 6 7 8] 数组中

任何想法?

谢谢

How do i go about seeing if my integer is in an array of integers...

eg i want to know if 7 is in an array of [ 1 3 4 5 6 7 8]

any ideas?

Thanks

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

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

发布评论

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

评论(3

一腔孤↑勇 2024-09-04 07:57:52

有多种方法可以做到这一点,具体取决于数组大小等因素 - 需要搜索的频率、需要添加到数组的频率等。一般来说,这是一个计算机科学问题。

更具体地说,我猜想有三种选择可能最适合您的需求。

  1. “暴力”:只需循环遍历数组寻找值。在 NSArray 上调用 containsObject: 将为您完成此操作。对于小数组来说很简单并且可能是最快的。
  2. 将数组复制到 集合< /a> 并使用 containsObject: 检查是否存在
  3. 将值保留在数组中,但对数组进行排序并实现您自己的 二分搜索 - 这可能并不像听起来那么复杂。

There are several ways to do this depending on factors such as size of the array - how often you need to search, how often you need to add to the array etc. In general this is a computer science problem.

More specifically I'd guess there are three options likely to best fit your needs.

  1. "Brute force": just loop through the array looking for the value. Calling containsObject: on the NSArray will do this for you. Simple and probably fastest for small array sizes.
  2. Copy the array into a set and use containsObject: to check for existence
  3. Keep the values in the array, but sort the array and implement your own binary search - which is probably not as complex as it sounds.
轮廓§ 2024-09-04 07:57:52

这取决于您拥有的数组的类型,是对象还是 C 数组。从你的标签来看,你有一个带有 NSIntegers 的 NSArray ,这是错误的。 NSIntegers 不是对象,不能放入 NSArray 中,除非将它们包装到一个对象中,例如 NSNumber。

NSArray

使用containsObject: 方法。

我不完全确定你如何将整数放入 NSArray 中。通常的方法是使用 NSNumber。

NSArray *theArray = [NSArray arrayWithObjects:[NSNumber numberWithInteger:1],
                                              [NSNumber numberWithInteger:7],
                                              [NSNumber numberWithInteger:3],
                                              nil];
NSNumber *theNumber = [NSNumber numberWithInteger:12];
/*
 * if you've got the plain NSInteger you can wrap it
 * into an object like this:
 * NSInteger theInt = 12;
 * NSNumber *theNumber = [NSNumber numberWithInteger:theInt];
 */
if ([theArray containsObject:theNumber]) {
    // do something
}

C 阵列

我怀疑您正在使用 C 阵列。在这种情况下,您必须编写自己的循环。

NSInteger theArray[3] = {1,7,3}
NSInteger theNumber = 12;
for (int i; i < 3; i++) {
    if (theArray[i] == theNumber) {
        // do something
        break; // don't do it twice
               // if the number is twice in it
    }
}

This depends on the type of array you have, if it's an object or a C array. Judging by your tags you've got an NSArray with NSIntegers, this would be wrong. NSIntegers are not objects and cannot be put into an NSArray, unless you wrap them into an object, for example an NSNumber.

NSArray

Use the containsObject: method.

I'm not entirely sure how you put your integers into an NSArray. The usual way to do this is to use NSNumber.

NSArray *theArray = [NSArray arrayWithObjects:[NSNumber numberWithInteger:1],
                                              [NSNumber numberWithInteger:7],
                                              [NSNumber numberWithInteger:3],
                                              nil];
NSNumber *theNumber = [NSNumber numberWithInteger:12];
/*
 * if you've got the plain NSInteger you can wrap it
 * into an object like this:
 * NSInteger theInt = 12;
 * NSNumber *theNumber = [NSNumber numberWithInteger:theInt];
 */
if ([theArray containsObject:theNumber]) {
    // do something
}

C-Array

I suspect you're using a C-Array. In that case you have to write your own loop.

NSInteger theArray[3] = {1,7,3}
NSInteger theNumber = 12;
for (int i; i < 3; i++) {
    if (theArray[i] == theNumber) {
        // do something
        break; // don't do it twice
               // if the number is twice in it
    }
}
‘画卷フ 2024-09-04 07:57:52
//assume these data, either from a method call or instance variables
int theArray[7] = {1,7,3,8,5,7,4};
int numberIWant = 8;

//this is the essence in a C-array, which you can easily use on ios
BOOL isNumberFound = NO;
for (int i; i < sizeof(theArray)/sizeof(int); i++) {
    if (theArray[i] == numberIWant) {
        isNumberFound = YES;
        break; //breaks the for loop               
    }
}
//return the bool, or otherwise check the bool

if (isNumberFound)
{
//do stuff
}
//assume these data, either from a method call or instance variables
int theArray[7] = {1,7,3,8,5,7,4};
int numberIWant = 8;

//this is the essence in a C-array, which you can easily use on ios
BOOL isNumberFound = NO;
for (int i; i < sizeof(theArray)/sizeof(int); i++) {
    if (theArray[i] == numberIWant) {
        isNumberFound = YES;
        break; //breaks the for loop               
    }
}
//return the bool, or otherwise check the bool

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