gpt4 book ai didi

java - 是否有一个 List 实现(在 Java 中)支持移位插入?

转载 作者:行者123 更新时间:2023-12-02 12:58:58 25 4
gpt4 key购买 nike

假设我有一个包含元素的列表

4,7,9,17,24

我想插入 11 但要保持它们有序。所以我想做一些类似的事情

list.add(3, 11),得到如下列表:

4,7,9,11,17,24

但如果我这样做,我就会把 17 替换为 11。你能帮忙吗?

最佳答案

add(int index, E element)方法应该做你想做的。 javadoc 是这样说的:

Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

如果没有,则您正在使用有缺陷的自定义 List 实现...或者您的应用程序没有执行您认为它正在执行的操作。 (也许您使用 list.set(3, 11)) ...)

关于java - 是否有一个 List 实现(在 Java 中)支持移位插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2978815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com