找出NSArray / NSMutableArray更改'索引 [英] Finding out NSArray/NSMutableArray changes' indices

查看:92
本文介绍了找出NSArray / NSMutableArray更改'索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 NSMutableArray oldArray 。现在,在一个点上,这个的NSMutableArray 对象被与另一种更新的的NSMutableArray ,其可以具有更多,更少或相同数量的元素作为前一个 NSMutableArray

I have a NSMutableArray oldArray. Now, at one point, this NSMutableArray object gets updated with another NSMutableArray, which may have more, less or same number of elements as the previous NSMutableArray.

我想比较旧数组和新数组的变化。我想要的是两个 NSArray s addedArray removedArray 哪个将包含已从旧数组中添加和/或删除的元素的索引。

I want to compare the old and the new array for changes. What I want are two NSArrays addedArray and removedArray which will contain the indices of the elements which have been added and/or removed from the old array.

通过一个例子,整个问题会更清楚:

This whole problem will be more clear with an example :

oldArray = {@"a",@"b",@"d",@"e",@"g"};

newArray = {@"a",@"c",@"d",@"e",@"f",@"h"};

因此,删除的对象在索引1和4处是@b和@g分别。并且在索引1,4和5处添加的对象是@c,@f和@h(删除第一个对象,然后添加)。

So, here the removed objects are @"b" and @"g" at indices 1 and 4 respectively. And the objects added are @"c", @"f" and @"h" at indices 1, 4 and 5 (first objects are removed, then added).

因此,

removedArray = {1,4};  and  addedArray = {1,4,5};

我想要一种有效的方法来获取这两个数组 - removedArray NSMutableArray 的c $ c>和 addedArray 。谢谢!如果问题不是很容易理解,我愿意提供更多信息。

I want an efficient way to get these two arrays - removedArray and addedArray from the old and new NSMutableArray. Thanks! If the problem is not very understandable, I'm willing to provide more information.

编辑1

也许我会更清楚地解释一下我想用它来做什么。

Perhaps it will be more clear if I explain what I want to use this for.

实际上我用它来更新UITableView方法 insertRowsAtIndexPaths removeRowsAtIndexPaths 通过实现代码如下之后动画被加载,从而使用户可以看到已删除的行出去新行进入.tableview存储用户可以添加或删除的收藏夹元素。添加一些收藏夹后删除一些;当用户返回收藏夹表格视图时,将显示动画。

Actually what I am using this for is updating a UITableView with methods insertRowsAtIndexPaths and removeRowsAtIndexPaths with animation after the tableview gets loaded, so that the user can see the removed rows go out and the new rows come in. The tableview stores the Favourites elements which the user can add or remove. So after adding some favorites and removing some; when the user comes back to the favourites tableview, the animations will be shown.

编辑2

之前应该已经提到过,但旧数组和新数组中的元素都是升序排列的。只有删除或添加的指标才有意义。订单无法更改。恩。 {@b,@a,@c,@d} 不能是数组。

Should have mentioned this earlier, but the elements in both the old and the new array will be in an ascending order. Only the indices of the removal or addition matters. The order cannot be changed. ex. {@"b",@"a",@"c",@"d"} cannot be an array.

推荐答案


我尝试使用循环和条件迭代旧数组和新数组,但它是否变得非常混乱和错误。

I have tried iterating through the old and the new arrays using loops and if conditions, but is it getting really messy and buggy.

这不是一个简单的问题。首先,请注意它可能有多种解决方案:

This is not a simple problem. First, note that it may have multiple solutions:

a b c d
b c d e

(a = {0,1,2,3},r = {0,1,2,3}}) (a = {3},r = {0})是有效的解决方案。您可能正在寻找的是 minimal 解决方案。

both (a={0, 1, 2, 3}, r={0, 1, 2, 3}) and (a={3}, r={0}) are valid solutions. What you are probably looking for is a minimal solution.

获得最小解决方案的一种方法是找到 最长公共子序列(LCS) 两个序列。查找LCS的算法将告诉您哪两个序列的元素属于LCS,哪些不属于LCS。不在LCS中的原始数组的每个元素的索引都进入 removed 数组;不在LCS中的新数组元素的索引进入添加的数组。

One way to get a minimal solution is by finding the Longest Common Subsequence (LCS) of the two sequences. The algorithm for finding LCS will tell you which elements of the two sequences belong to the LCS, and which do not. Indexes of each element of the original array that is not in LCS go into the removed array; indexes of elements of the new array that are not in LCS go into the added array.

以下是一些示例(我将LCS的元素括号):

Here are a few examples (I parenthesized the elements of LCS):

 0  1   2   3   4   5
(a) b  (d) (e)  g
(a) c  (d) (e)  f   h

不在LCS中的 old 的项目是1和4;不在LCS中的 new 项目分别为1,4和5。

The items of old not in LCS are 1 and 4; the items of new not in LCS are 1, 4, and 5.

这是另一个例子:

 0   1   2   3
 a  (b) (c) (d)
(b) (c) (d)  e

现在已添加 3 已删除 0

这篇关于找出NSArray / NSMutableArray更改'索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆