w3resource

Java ArrayDeque Class: addLast()Method

public void addLast(E e)

The addLast() method is used to insert a given element at the end of this deque.

Note: This method is equivalent to add(E).

Package: java.util

Java Platform: Java SE 8

Syntax: addLast(E e)

Parameters:

Name Description
e    The element to add.

Return Value Type: void

Throws:

NullPointerException - if the specified element is null

Pictorial Presentation

Java ArrayDeque Class: addLast() Method

Example: Java ArrayDeque Class: addLast() Method

import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
   public static void main(String[] args) {
      
      // Create an empty array deque with an initial capacity 3
      Deque<Integer> deque = new ArrayDeque<Integer>(3);

      // Use add() method to add elements in the deque
      deque.add(10);
      deque.add(20);
      deque.add(30);

      // Use addLast() method to add 3 elements 
      // at the last of the deque and print all the elements
      deque.addLast(40);
       for (Integer n : deque) {
         System.out.println("Number = " + n);
       }
      deque.addLast(50);
      System.out.println();
       for (Integer n : deque) {
         System.out.println("Number = " + n);
       }
      deque.addLast(60);
      System.out.println();
       for (Integer n : deque) {
         System.out.println("Number = " + n);
      }
  }
}

Output:

Number = 10
Number = 20
Number = 30
Number = 40

Number = 10
Number = 20
Number = 30
Number = 40
Number = 50

Number = 10
Number = 20
Number = 30
Number = 40
Number = 50
Number = 60

Java Code Editor:

Previous: addFirst Method
Next: clear Method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-tutorial/util/arraydeque/java_arraydeque_addlast.php