如何获取数组中项的索引

时间:2022-06-13 17:27:07

I am having array called stored and I want to access their indexes so that I can select them individually to do my operations.

我有一个名为stored的数组,我想访问它们的索引,以便我可以单独选择它们来执行我的操作。

If I want to print their indexes, what should I do?

如果我想打印他们的索引,我该怎么办?

6 个解决方案

#1


49  

use NSArray's indexOfObject: method. Such as the following:

使用NSArray的indexOfObject:方法。如下:

NSUInteger fooIndex = [someArray indexOfObject: someObject];

#2


5  

int totalElements = [anArray count];
for(int i=0; i < totalElements; i++)
{
    data = [myArray indexOfObject:i];
}

The above code will give you the data if you pass in the index

如果传入索引,上面的代码将为您提供数据

NSString *haystackText = @"Hello World";
int totalElements = [anArray count];
for(int i=0; i < totalElements; i++)
{
    BOOL result = [haystackText caseInsensitiveCompare:[myArray objectAtIndex:i] == NSOrderedSame;
    if(result)
    {
         NSUInteger fooIndex = [myArray indexOfObject: haystackText];
         return fooIndex;
    }
}

The code above will first find the element in the array and if it exists then return the index.

上面的代码将首先找到数组中的元素,如果存在则返回索引。

#3


2  

[array indexOfObject:object]

returns index of "object"

返回“对象”的索引

#4


2  

You can use indexOfObject method to get the index of element.

您可以使用indexOfObject方法获取元素的索引。

for example

例如

This will give you index of your object

这将为您提供对象的索引

  NSInteger index = [yourArray indexOfObject:objectName];

This worked for me. Hope this helps.

这对我有用。希望这可以帮助。

#5


0  

You can get a list of the indexes by using the count method which returns the number of elements in the array:

您可以使用count方法获取索引列表,该方法返回数组中元素的数量:

int numElements = [myArray count];

I'll leave it as an exercise to print out the actual index values :)

我会把它作为练习来打印出实际的索引值:)

#6


0  

This article has some great examples of how to iterate over an array. Then, inside the loop, you can use NSLog to print the index.

本文提供了一些如何迭代数组的很好的例子。然后,在循环内部,您可以使用NSLog打印索引。

#1


49  

use NSArray's indexOfObject: method. Such as the following:

使用NSArray的indexOfObject:方法。如下:

NSUInteger fooIndex = [someArray indexOfObject: someObject];

#2


5  

int totalElements = [anArray count];
for(int i=0; i < totalElements; i++)
{
    data = [myArray indexOfObject:i];
}

The above code will give you the data if you pass in the index

如果传入索引,上面的代码将为您提供数据

NSString *haystackText = @"Hello World";
int totalElements = [anArray count];
for(int i=0; i < totalElements; i++)
{
    BOOL result = [haystackText caseInsensitiveCompare:[myArray objectAtIndex:i] == NSOrderedSame;
    if(result)
    {
         NSUInteger fooIndex = [myArray indexOfObject: haystackText];
         return fooIndex;
    }
}

The code above will first find the element in the array and if it exists then return the index.

上面的代码将首先找到数组中的元素,如果存在则返回索引。

#3


2  

[array indexOfObject:object]

returns index of "object"

返回“对象”的索引

#4


2  

You can use indexOfObject method to get the index of element.

您可以使用indexOfObject方法获取元素的索引。

for example

例如

This will give you index of your object

这将为您提供对象的索引

  NSInteger index = [yourArray indexOfObject:objectName];

This worked for me. Hope this helps.

这对我有用。希望这可以帮助。

#5


0  

You can get a list of the indexes by using the count method which returns the number of elements in the array:

您可以使用count方法获取索引列表,该方法返回数组中元素的数量:

int numElements = [myArray count];

I'll leave it as an exercise to print out the actual index values :)

我会把它作为练习来打印出实际的索引值:)

#6


0  

This article has some great examples of how to iterate over an array. Then, inside the loop, you can use NSLog to print the index.

本文提供了一些如何迭代数组的很好的例子。然后,在循环内部,您可以使用NSLog打印索引。