Untitled
unknown
plain_text
10 months ago
977 B
26
Indexable
public class DequeExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
// Adding elements to the front
deque.addFirst("one");
deque.addFirst("two");
// Adding elements to the end
deque.addLast("three");
deque.addLast("four");
// Printing the deque
System.out.println("Deque: " + deque); // Output: Deque: [two, one, three, four]
// Removing elements from the front
String firstElement = deque.removeFirst();
System.out.println("Removed from front: " + firstElement); // Output: Removed from front: two
// Removing elements from the end
String lastElement = deque.removeLast();
System.out.println("Removed from end: " + lastElement); // Output: Removed from end: four
System.out.println("Deque after removals: " + deque); // Output: Deque after removals: [one, three]
}
}Editor is loading...
Leave a Comment