JAVA simple Deque Interface example

import java.util.*;

class Deque {
public static void main(String[] arguments){

ArrayDeque<String> ourDeque = new ArrayDeque<String>();

// fill our deque interface
String[] alph = { “a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”,”i”,”j”,”k”,”l”,”m”,”n”,”o”,”p”,”q”,”r”,”s”,”t”,”u”,”v”,”w”,”x”,”y”,”z” };
for( int i = 0; i < alph.length; i++){
ourDeque.addFirst( alph[i] );
}

// foreach
for (String str : ourDeque) {
System.out.print(str);
}

System.out.println(“\n”);

// iterator
//ArrayDeque<String> ourDeque = new ArrayDeque<String>();
for (Iterator<String> iter = ourDeque.iterator(); iter.hasNext(); ) {
System.out.print(iter.next());
}

System.out.println(“\n”);

// Let’s do it forward though
int holdSize = ourDeque.size();
for ( int j = 0; j < holdSize; j++){
String getOne = ourDeque.removeLast();
System.out.print(getOne);
}

System.out.println(“\n”);
}
}

Leave a Reply

You must be logged in to post a comment.