国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home 類庫下載 java類庫 Java-LinkedList source code principle analysis, and constructing a queue through LinkedList

Java-LinkedList source code principle analysis, and constructing a queue through LinkedList

Oct 11, 2016 pm 04:27 PM

Here we introduce the simplest linked list, LinkedList;

Look at the add() method:

public boolean add(E e) {
        linkLast(e);        return true;
    }
void linkLast(E e) {
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }

The principle of add is:

1. First get the last node of the linked list.

2. Insert the new node after the last node.

3.The last attribute of linkedList redirects to the last node.

4. If this node is the first node and there is no previous node, then point the first attribute of the linkedList to the new node; if not, point the next attribute of the previous node to the node.

Use LinkedList to build a first-in-first-out queue:

offer() method to join the queue: use the add() method to insert the node at the end.

public boolean offer(E e) {
        return add(e);
    }

poll() method dequeue: remove the queue from the head of the linked list

public E poll() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

Use LinkedList to build a last-in-first-out queue:

push() method enqueue: insert the node at the first

public void addFirst(E e) {
        linkFirst(e);
    }

private void linkFirst(E e) {
        final Node<E> f = first;
        final Node<E> newNode = new Node<>(null, e, f);
        first = newNode;
        if (f == null)
            last = newNode;
        else
            f.prev = newNode;
        size++;
        modCount++;
    }

pop() method dequeue : Remove from the queue starting from the head of the linked list

public E pop() {
        return removeFirst();
    }

public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

private E unlinkFirst(Node<E> f) {
        // assert f == first && f != null;
        final E element = f.item;
        final Node<E> next = f.next;
        f.item = null;
        f.next = null; // help GC
        first = next;
        if (next == null)
            last = null;
        else
            next.prev = null;
        size--;
        modCount++;
        return element;
    }

The last thing to note is: LinkedList is thread-unsafe. If you need thread safety, please use synchronized locking, or use vector, or use the java.util.concurrent package.

If you need to avoid ConcurrentModificationException when a thread traverses the List, there are 3 solutions.

1. Use synchronized locking when traversing the List;

2. Use CopyOnWriteArrayList under the java.util.concurrent package. Every time you use the List, you are actually using a copy of the List.

3. Use the foreach method in Jdk8, but this method only accepts lambda expressions

list.forEach(item -> {
                    System.out.println("遍歷元素:" + item);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276