将元素添加到List集合的第一位

list.add(1, object)

看一下add方法的注释

/**
     * 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).
     *
     * @param index index at which the specified element is to be inserted
     * @param element element to be inserted
     * @throws UnsupportedOperationException if the <tt>add</tt> operation
     *         is not supported by this list
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this list
     * @throws NullPointerException if the specified element is null and
     *         this list does not permit null elements
     * @throws IllegalArgumentException if some property of the specified
     *         element prevents it from being added to this list
     * @throws IndexOutOfBoundsException if the index is out of range
     *         (<tt>index &lt; 0 || index &gt; size()</tt>)
     */
    void add(int index, E element);

翻译(有不对的地方及时指正)

   /**
     * 在List中指定的位置上插入指定的元素
     * (可选操作)。  将当前处于该位置的元素
     * (如果有的话) 和任何后续的元素向右移 (增加他们的下标)。
     *
     * @param index 指定被插入元素的下标
     * @param element 要插入的元素
     * @throws UnsupportedOperationException 如果此List不支持添加操作
     * @throws ClassCastException 如果指定元素的类阻止它被添加到这个List中
     * @throws NullPointerException 如果指定元素为null并且该List不允许null元素
     * @throws IllegalArgumentException 如果指定元素的某些属性防止它被添加到这个List中
     * @throws IndexOutOfBoundsException 如果下标超出范围(下标 < 0 || 下标 > size())
     */
     void add(int index, E element);

总结:对API不熟

转载于:https://my.oschina.net/u/2968713/blog/777462

在许多编程语言中,将一个元素添加到列表(数组、链表等数据结构)的首位是一个常见需求。这里以 PythonJava 为例来说明如何实现这一功能。 ### 使用 Python 添加元素到列表首位 在 Python 中最常用的数据结构之一就是`list`。为了向已有的列表头部插入一个新的项,可以使用内置方法 `insert()` 或者通过切片的方式完成: **示例代码:** ```python # 创建一个初始列表 my_list = [2, 3, 4] # 方法一:使用 insert() 函数 my_list.insert(0, 1) # 参数分别为索引位置及新加入的对象 print("Using insert():", my_list) # 输出结果将是: Using insert(): [1, 2, 3, 4] ``` 另外一种简单且直观的做法是直接加上新的元素形成一个包含旧内容的新列表: ```python new_element = 1 my_list = [new_element] + my_list print("By concatenation:", my_list) ``` 这两种方法都能达到预期效果,选择哪种取决于具体的性能考量和个人偏好。 ### 在 Java 中前置添加元素List 首位 对于 Java 而言,如果希望在一个现有的 ArrayList 类型实例前面增加一项,通常会采用以下几种办法: - **ArrayList**: 对于较大规模集合而言效率较低,因为每次都需要移动后续的所有项目来腾出空间给新增加的部分。 ```java import java.util.ArrayList; //... ArrayList<Integer> myList = new ArrayList<>(); myList.add(0); // 原有列表 { } myList.add(1); System.out.println(myList); // 打印当前状态 [1] myList.add(0, -1); // 向第一个位置处追加 -1 System.out.println(myList); // 最终输出 [-1, 1] ``` - **LinkedList**: 更适合频繁地从两端增删节点的情况,因为它内部维护着双向指针机制使得此类操作更高效些。 ```java import java.util.LinkedList; public class Main { public static void main(String[] args){ LinkedList<String> linkedListExample = new LinkedList<>(); // Add elements to the end of list initially. linkedListExample.add("Apple"); linkedListExample.add("Banana"); System.out.println(linkedListExample); // Insert element at first position. linkedListExample.addFirst("Orange"); System.out.println(linkedListExample); } } ``` 上面的例子展示了如何使用不同类型的容器类并选取合适的方法来进行所需的操作。 总之,在处理需要经常变动大小尤其是前端插入频率较高的场景时,请务必考虑到所选用工具的特点以及由此带来的潜在开销影响!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值