我需要能够从列表中删除项目并将其插入,同时保持相同的顺序。我的列表从最初的一组项目开始。无法插入新项目。所以,增加的项目和最大的项目计数保持不变。
示例:
我的初始列表如下
0=201, 1=402, 2=952, 3=101, 4=-54, 5=0 如果我在2,4个位置移除物品,我有
0=201, 1=402, 3=101, 5=0 但是,当我将已删除位置的项添加回列表时,{2,4}
0=201, 1=402, 3=101, 5=0, 2=952, 4=-54我希望以最初的顺序添加这些项目。即
0=201, 1=402, 2=952, 3=101, 4=-54, 5=0 它们必须按照它们被移除的原始顺序。这只是一个例子,这些值不能排序。它们是按加上去的顺序排列的。
如果建议使用正本备份列表,如何确定当前列表的正确相邻位置?因此,如果原始列表有6个项,并且值-54位于4索引处。我从列表中删除{2,3,4}项。新列表的大小更改为3。我无法将原始列表中的4索引项添加到4索引处的新列表中。新列表将只有{0,1,2}作为有效位置。再说了,这根本不应该维持秩序。
因此,问题是,我如何删除元素并将其添加到列表中,以确保订单得到保持?
同样,这应该用LinkedList来完成吗?如果是,怎么做?
编辑:原始列表是一个ArrayList。我不能修改这种类型。当然,在确定正确的索引之后,我需要从另一个集合中为它提供条目。
发布于 2020-04-03 10:01:25
import org.junit.Test
class Fixing60945040 {
private val originalItems = mutableListOf(
201, 402, 952, 101, -54, 19
)
/**
* a backup map of original items
*
* Why LinkedHashMap? HashMap works with access-order implementation.
* Items do not keep insertion order in HashMap. So, inserting original
* items will look like: 402, 19, 101, -54, 952, 201
*
* LinkedHashMap keeps insertion-order by default.
*
* Map<Int, Boolean> = (value, hidden)
*
* So, items like 201, 402 etc are keys for the map
* and boolean tells us if this item is visible
*/
private lateinit var backupMap: LinkedHashMap<Int, Boolean>
/**
* create a backup of original items
*/
private fun createBackupMap(): LinkedHashMap<Int, Boolean> {
val backupMap = LinkedHashMap<Int, Boolean>()
for (item in originalItems) {
backupMap[item] = true
}
return backupMap
}
private fun removeItem(item: Int) {
println("Removing: $item")
assert(originalItems.contains(item)) { "Can't remove an item that doesn't exist in the list." }
// remove this value from original list
val modified = originalItems.remove(item)
if (modified) {
// item was removed. Now, mark this item as hidden in the backup map
backupMap[item] = false
}
}
private fun insertItem(item: Int) {
println("Inserting: $item")
assert(!originalItems.contains(item)) { "Can't insert duplicate item." }
assert(backupMap.containsKey(item)) { "Can't insert unknown new item." }
// mark this item as visible in the backup map
backupMap[item] = true
// to know the insertion index, we need to iterate through
// backup map
var insertionIndex = 0
for (entry in backupMap) {
if (!entry.value /* !isVisible */ ) {
/* not visible, this item is marked as hidden; ignoring */
continue
}
// Is this key same as the item to be inserted?
if (entry.key == item) {
// It is. Insert the item.
originalItems.add(insertionIndex, item)
break
} else {
// It isn't, increase the index; check the next key.
insertionIndex++
}
}
}
@Test
fun test() {
backupMap = createBackupMap()
println("list: $originalItems \n")
removeItem(952)
removeItem(-54)
println("list: $originalItems \n")
removeItem(101)
println("list: $originalItems \n")
insertItem(-54)
println("list: $originalItems \n")
insertItem(101)
println("list: $originalItems \n")
insertItem(952)
println("list: $originalItems \n")
}
}如果任何人发现任何性能改进,请评论。如果您认为有更好的方法,请添加一个答案。
发布于 2020-03-31 08:07:30
在开始时,请使用Map <Index, Map<oldValue, NewValue>> (根据您的要求)提供一个原始文件的备份列表。如果从ArrayList中删除任何项,则在Map中将新值标记为null。
https://stackoverflow.com/questions/60945040
复制相似问题