Class IntDeque


  • public final class IntDeque
    extends java.lang.Object
    A deque of integers that increases its capacity as necessary. Since it's a deque, it can be used as a FIFO (First-In-First-Out) queue and as a LIFO (Last-In-First-Out) stack.
    See Also:
    Deque
    • Constructor Summary

      Constructors 
      Constructor Description
      IntDeque()
      Creates a new IntDeque with an initial capacity of 4.
      IntDeque​(int initialCapacity)
      Creates a new IntDeque with the specified initial capacity.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addFirst​(int element)
      Inserts an element before the head of this deque.
      void addLast​(int element)
      Inserts an element at the tail of this deque.
      void clear()
      Clears this deque.
      void compact()
      Compacts this deque, minimizing its size in memory.
      int get​(int index)
      Gets the element at the specified index of this deque, without removing it.
      int getFirst()
      Gets the first element (head) of this deque without removing it.
      int getLast()
      Gets the last element of this deque without removing it.
      boolean isEmpty()  
      int removeFirst()
      Retrieves and removes the first element (head) of this deque.
      int removeLast()
      Retrieves and removes the last element of this deque.
      int size()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • IntDeque

        public IntDeque()
        Creates a new IntDeque with an initial capacity of 4.
      • IntDeque

        public IntDeque​(int initialCapacity)
        Creates a new IntDeque with the specified initial capacity. The capacity must be positive and non-zero.
        Parameters:
        initialCapacity - the initial capacity, strictly positive
    • Method Detail

      • clear

        public void clear()
        Clears this deque. After a call of this method, the size of the deque will be zero.
      • isEmpty

        public boolean isEmpty()
        Returns:
        true if the deque is empty, false if it's not
      • size

        public int size()
        Returns:
        the size of the deque
      • compact

        public void compact()
        Compacts this deque, minimizing its size in memory.
      • addFirst

        public void addFirst​(int element)
        Inserts an element before the head of this deque. The deque increases its capacity if necessary.
        Parameters:
        element - the element to add
      • addLast

        public void addLast​(int element)
        Inserts an element at the tail of this deque. The deque increases its capacity if necessary.
        Parameters:
        element - the element to add
      • get

        public int get​(int index)
        Gets the element at the specified index of this deque, without removing it.

        The index is relative to the head: the first element is at index 0, the next element is at index 1, etc.

        Parameters:
        index - the index of the element, relative to the head
        Returns:
        the element at the specified index
        Throws:
        java.util.NoSuchElementException - if the deque contains less than index+1 elements
      • getFirst

        public int getFirst()
        Gets the first element (head) of this deque without removing it.
        Returns:
        the first element of this deque
        Throws:
        java.util.NoSuchElementException - if the deque is empty
      • getLast

        public int getLast()
        Gets the last element of this deque without removing it.
        Returns:
        the last element of this deque
        Throws:
        java.util.NoSuchElementException - if the deque is empty
      • removeFirst

        public int removeFirst()
        Retrieves and removes the first element (head) of this deque.
        Returns:
        the first element of this deque
        Throws:
        java.util.NoSuchElementException - if the deque is empty
      • removeLast

        public int removeLast()
        Retrieves and removes the last element of this deque.
        Returns:
        the last element of this deque
        Throws:
        java.util.NoSuchElementException - if the deque is empty