Tutorialspoint

Java Array Deque Offer First

package com.tutorialspoint;

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

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

      // use add() method to add elements in the deque
      deque.add(25);
      deque.add(30);
      deque.add(20);
      deque.add(18);        

      // this will insert 10 at the front
      deque.offerFirst(10);

      // printing all the elements available in deque
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }
   }
}

Java Array Deque Offer

package com.tutorialspoint;

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

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

      // use add() method to add elements in the deque
      deque.add(25);
      deque.add(30);
      deque.add(20);
      deque.add(18);        

      // this will insert 50 at the end
      deque.offer(50);

      // printing all the elements available in deque
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }
   }
}

Java Array Deque Iterator

package com.tutorialspoint;

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

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

      // use add() method to add elements in the deque
      deque.add(15);
      deque.add(22);
      deque.add(25);
      deque.add(20);

      // iterator() is used to print all the elements
      // next() returns the next element on each iteration
      System.out.println("printing elements using iterator:");
      for(Iterator itr = deque.iterator();itr.hasNext();)  {
         System.out.println(itr.next());
      }
   }
} 

Java Array Deque IsEmpty

package com.tutorialspoint;

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

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

      // create an empty array deque with an initial capacity
      Deque<Integer> deque = new ArrayDeque<Integer>(8);

      // use add() method to add elements in the deque
      deque.add(25);
      deque.add(30);
      deque.add(20);
      deque.add(18);        

      boolean retval = deque.isEmpty();
      if (retval == true) {
         System.out.println("deque is empty");
      } else {
         System.out.println("deque is not empty");
      }

      // printing all the elements available in deque
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }
   }
}

Java Array Deque GetLast

package com.tutorialspoint;

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

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

      // create an empty array deque with an initial capacity
      Deque<Integer> deque = new ArrayDeque<Integer>(8);

      // use add() method to add elements in the deque
      deque.add(25);
      deque.add(30);
      deque.add(20);
      deque.add(18);        

      // let us print all the elements available in deque
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }

      // getLast() will retrieve element at last position
      int retval = deque.getLast();
      System.out.println("Retrieved Element is = " + retval);
   }
}

Java Array Deque GetFirst

package com.tutorialspoint;

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

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

      // use add() method to add elements in the deque
      deque.add(15);
      deque.add(30);
      deque.add(20);
      deque.add(18);        

      // let us print all the elements available in deque
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }

      // getFirst() will retrieve element at first(head) position
      int retval = deque.getFirst();
      System.out.println("Retrieved Element is = " + retval);
   }
}

Java Array Deque Element

package com.tutorialspoint;

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

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque with an initial capacity
      ArrayDeque<Integer> deque = new ArrayDeque<Integer>(8);

      // use add() method to add elements in the deque
      deque.add(25);
      deque.add(30);
      deque.add(20);
      deque.add(18);        

      // let us print all the elements available in deque
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }

      // it will retrive element at first(head) position
      int retval = deque.element();
      System.out.println("Retrieved Element is = " + retval);
   }
}

Java Array Deque DescendingIterator

package com.tutorialspoint;

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

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

      // use add() method to add elements in the deque
      deque.add(15);
      deque.add(22);
      deque.add(25);
      deque.add(20);

      //iterator() is used to print all the elements
      //next() returns the next element on each iteration
      System.out.println("printing elements using iterator:");
       
      for(Iterator itr = deque.iterator();itr.hasNext();)  {
         System.out.println(itr.next());
      }

      //descendingIterator() is used to print the elements in reverse order
      //next() returns the next element on each iteration
      System.out.println("printing elements in reverse order:");
       
      for(Iterator descItr = deque.descendingIterator();descItr.hasNext();) {
         System.out.println(descItr.next());
      }
   }
 }

Java Array Deque Contains

package com.tutorialspoint;

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

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

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

      // let us print all the elements available in deque
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }

      // deque contains element 10
      boolean retval = deque.contains(10); 
        
      if (retval == true) {
         System.out.println("element 10 is contained in the deque");
      } else {
         System.out.println("element 10 is not contained in the deque");
      }

      // deque does not contain element 25
      boolean retval2 = deque.contains(25);

      if (retval2 == true) {
         System.out.println("element 25 is contained in the deque");
      } else {
         System.out.println("element 25 is not contained in the deque");    
      }
   }
} 

Java Array Deque Clone

package com.tutorialspoint;

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

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque with an initial capacity
      ArrayDeque<Integer> deque1 = new ArrayDeque<Integer>(8);

      // use add() method to add elements in the deque
      deque1.add(15);
      deque1.add(30);
      deque1.add(20);
      deque1.add(18);
         
      // clone the first deque,
      Deque<Integer> deque2 = deque1.clone();

      // let us print all the elements available in deque2
      // now deque2 should have similar elements to deque1.
      for (Integer number : deque2) {
         System.out.println("Number = " + number);
      }
   }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.