Java LinkedList offerFirst()方法

java.util.LinkedList.offerFirst(E e) 将指定的元素插入LinkedList的开头。

1 语法

public boolean offerFirst(E e)

2 参数

e:要添加的元素

3 返回值

返回true。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.LinkedList.offerFirst(E e)方法的例子
 */
import java.util.*;

public class Demo {
    public static void main(String[] args) {

        // create a LinkedList
        LinkedList list = new LinkedList();

        // add some elements
        list.add("Hello");
        list.add(2);
        list.add("Chocolate");
        list.add("10");

        // print the list
        System.out.println("LinkedList:" + list);

        // offer a new element as the head of the list
        list.offerFirst("Element");

        // print the new list
        System.out.println("LinkedList:" + list);
    }
}

输出结果为:

LinkedList:[Hello, 2, Chocolate, 10]
LinkedList:[Element, Hello, 2, Chocolate, 10]

热门文章

优秀文章