【问题标题】:Maintain initial list items order while adding or removing items in it维护初始列表项顺序,同时添加或删除其中的项目
【发布时间】:2023-03-10 03:58:01
【问题描述】:

我需要能够从列表中删除项目并将它们重新插入,同时保持相同的顺序。我的清单从最初的一组项目开始。无法插入新项目。因此,added-items 和 max-item-count 保持不变。

例子:

我的初始列表如下

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。我无法修改这种类型。当然,在确定正确的索引后,我需要从另一个集合中提供项目。

【问题讨论】:

    标签: java list kotlin linked-list


    【解决方案1】:

    首先备份原件列表,但在Map <Index, Map<oldValue, NewValue>>(根据您的要求)。如果您从 ArrayList 中删除任何项目,请在 Map 中将新值标记为 null

    【讨论】:

    • 抱歉,无法为每个条目创建地图。
    【解决方案2】:
    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")
        }
    }
    

    如果有人发现任何性能改进,请发表评论。如果您认为有更好的方法,请添加答案。

    【讨论】:

      猜你喜欢
      • 2010-12-05
      • 2011-01-17
      • 1970-01-01
      • 2014-05-05
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 2014-07-09
      相关资源
      最近更新 更多