-
CSR
-
Resolution: Unresolved
-
P4
-
None
-
None
-
source
-
minimal
-
These are new methods so the impact to existing code should be minimal.
-
Java API
-
JDK
Summary
Add two new methods to ArrayDeque
:
public E get(int index)
public E set(int index, E element)
Problem
Because it is backed by an array data structure, the ArrayDeque
class has the ability to get and replace any element in the list accessed by index in constant time. However, this capability is not exposed in the API.
This is frustrating to developers because a common application for ArrayDeque
is when you want a "double-ended ArrayList
", that is, you want an ArrayList
but you also have the need to append elements to both ends of the list efficiently. But if you use ArrayDeque
, you lose the essential ability of ArrayList
to access elements by index in constant time.
An ArrayList
allows appending elements to the end of the list in amortized constant time, but not the front of the list. ArrayDeque
supports doing so to both ends, but this is not exposed in the API.
Solution
Add the equivalent of the List
methods get(int)
and set(int, E)
to the ArrayDeque
API.
Specification
--- a/src/java.base/share/classes/java/util/ArrayDeque.java
+++ b/src/java.base/share/classes/java/util/ArrayDeque.java
@@ -351,6 +351,50 @@
return true;
}
+ /**
+ * {@return the element at the specified position in this deque}
+ *
+ * @param index index of the element to return
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * ({@code index < 0 || index >= size()})
+ * @since $N
+ */
+ public E get(int index) {
+ // implementation...
+ }
+
+ /**
+ * Replaces the element at the specified position in this deque
+ * with the specified element.
+ *
+ * @param index index of the element to replace
+ * @param element element to be stored at the specified position
+ * @return the element previously at the specified position
+ * @throws ClassCastException if the class of the specified element
+ * prevents it from being added to this deque
+ * @throws NullPointerException if the specified element is null
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * ({@code index < 0 || index >= size()})
+ * @since $N
+ */
+ public E set(int index, E element) {
+ // implementation...
+ }
+
/**
* @throws NoSuchElementException {@inheritDoc}
*/
- csr of
-
JDK-8143850 Add indexed get() and set() methods to ArrayDeque
-
- Open
-