ios 获取数组中对象的索引以在其他数组中查找对应的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6393593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get index of object in array to look up corresponding object in other array
提问by Preston
I have two arrays. One is an array of names and the other is an array made up of strings titled "Yes" or "No". The index path of each name in the "name" array corresponds with the same index path in the "Yes/No" array. For example:
我有两个数组。一个是名称数组,另一个是由标题为“是”或“否”的字符串组成的数组。每个名称的与“是/否”阵列中的相同的索引路径中的“名称”阵列对应的索引路径。例如:
Names Array | Yes/No Array
Person 1 | Yes
Person 2 | No
Person 3 | Yes
What would be the easiest way to look up a person's name (possibly getting the index path of it) and check whether they are "Yes" or "No" in the "Yes/No" array?
查找一个人的名字(可能获取它的索引路径)并检查他们在“是/否”数组中是“是”还是“否”的最简单方法是什么?
Also, I'm not sure if "index path" is the right term to use. If it isn't, I mean the number that an object is in an array.
另外,我不确定“索引路径”是否是正确的使用术语。如果不是,我的意思是对象在数组中的数量。
回答by PengOne
NSArray
has a method called indexOfObject
that will return either the lowest index whose corresponding array value is equal to anObject or NSNotFound if no such object is found. If your array of names is unsorted, then use this to get the index that you can then plug in to the Yes/No array. That is, something along these lines:
NSArray
有一个方法被调用indexOfObject
,如果没有找到这样的对象,它将返回其对应数组值等于 anObject 或 NSNotFound 的最低索引。如果您的名称数组未排序,则使用它来获取索引,然后您可以将其插入是/否数组。也就是说,沿着这些路线的东西:
NSString *answer = nil;
NSUInteger index = [namesArray indexOfObject:@"John Smith"];
if (index != NSNotFound) {
answer = [yesNoArray objectAtIndex:index];
}
return answer;
Because Bavarious asks questions where I assume, here's a better way when the array of names is sorted alphabetically.
由于Bavarious问的问题,我认为,这里的时名称数组按字母顺序排序更好的方法。
int index = [self findName:@"John Smith"];
NSString *answer = nil;
if (index >= 0) {
answer = [yesNoArray objectAtIndex:index];
}
return answer;
where the function findName
is a simple binary search:
其中函数findName
是一个简单的二分搜索:
-(int)findName:(NSString *)name {
int min, mid, max;
NSComparisonResult comparisonResult;
min = 0;
max = [namesArray count]-1;
while (min <= max) {
mid = min + (max-min)/2;
comparisonResult = [name compare:[namesArray objectAtIndex:mid]];
if (comparisonResult == NSOrderedSame) {
return mid;
} else if (comparisonResult == NSOrderedDescending) {
min = mid+1;
} else {
max = mid-1;
}
}
return -1;
}
回答by Caleb
Trying to keep two arrays synchronized is just asking for trouble. It can be done, of course, but whenever you modify one array, you have to remember to make a corresponding change to the other. Do yourself a favor and avoid that entire class of bugs by rethinking the way you're storing data.
试图保持两个数组同步只是自找麻烦。当然可以,但是每当你修改一个数组时,你必须记住对另一个数组进行相应的更改。帮自己一个忙,通过重新思考存储数据的方式来避免整类错误。
In this case, you've got a {person, boolean} pair. One option is to store each pair as a dictionary, and then keep an array of those dictionaries. This would be a particularly good plan if you might expand the number of pieces of data beyond the two that you have. Another option would be to just use a dictionary where keys are person names and the values are your yes/no values. This makes the answer to your question very simple:
在这种情况下,您有一个 {person, boolean} 对。一种选择是将每一对存储为字典,然后保留这些字典的数组。如果您可以将数据的数量扩展到您拥有的两个之外,这将是一个特别好的计划。另一种选择是只使用字典,其中键是人名,值是您的是/否值。这使您的问题的答案非常简单:
NSString *yesOrNo = [personDictionary objectForKey:personName];
Getting back to your original question, where you still have the two arrays, the easiestthing to do is to iterate over the person array until you find the person you're looking for, get the index of that name, and then look up the corresponding value in the yes/no array:
回到最初的问题,您仍然拥有两个数组,最简单的方法是遍历 person 数组,直到找到您要查找的人,获取该姓名的索引,然后查找是/否数组中的对应值:
for (person in peopleArray) {
if ([person isEqualToString:thePersonYoureLookingFor]) {
yesNoValue = [yesNoArray objectAtIndex:[peopleArray indexOfObject:person];
break;
}
}
That's fine if the number of people in the list isn't too large. If the list could be large, then you'll want to keep the person array sorted so that you can do a binary search. The trouble there, though, is that you're yes/no array is separate, so sorting the personArray while keeping the yes/no array in the right order becomes complicated.
这很好,如果人在列表中的号码不会太大。如果列表可能很大,那么您需要对 person 数组进行排序,以便您可以进行二分查找。但是,那里的问题是,yes/no 数组是分开的,因此在保持 yes/no 数组正确顺序的同时对 personArray 进行排序变得很复杂。
回答by Dinesh_
You can also use below of the code, May its useful to you,
您也可以使用下面的代码,可能对您有用,
NSSortDescriptor *_lastDescriptor = [[NSSortDescriptor alloc] initWithKey:@"" ascending:YES];
NSArray *_lastArray = [NSArray arrayWithObject:_lastDescriptor];
firstCharacterArray = (NSMutableArray *)[[nameIndexesDictionary allKeys]
sortedArrayUsingDescriptors:_lastArray];
//firstCharacterArray = (NSMutableArray *)[[nameIndexesDictionary allKeys]
sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
for (NSString *eachlastIndex in firstCharacterArray)
{
NSSortDescriptor *lastDescriptor = [[NSSortDescriptor alloc] initWithKey:@""
ascending:YES];
//selector:@selector(localizedCaseInsensitiveCompare:)] ;
NSArray *descriptorslast = [NSArray arrayWithObject:lastDescriptor];
[[nameIndexesDictionary objectForKey:eachlastIndex]
sortUsingDescriptors:descriptorslast];
[lastDescriptor release];
}
回答by Pradumna Patil
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];
To see the corresponding element from another array
从另一个数组中查看相应的元素
[anotherArray objectAtIndex:index];
This worked for me. Hope this helps.
这对我有用。希望这可以帮助。