CircularArray
class CircularArray<E : Any!>
kotlin.Any | |
↳ | androidx.collection.CircularArray |
CircularArray is a generic circular array data structure that provides O(1) random read, O(1) prepend and O(1) append. The CircularArray automatically grows its capacity when number of added items is over its capacity.
Summary
Public constructors |
|
---|---|
<init>() Creates a circular array with default capacity. |
|
Creates a circular array with capacity for at least |
Public methods |
|
---|---|
Unit |
addFirst(e: E) Add an element in front of the CircularArray. |
Unit |
addLast(e: E) Add an element at end of the CircularArray. |
Unit |
clear() Remove all elements from the CircularArray. |
E |
Get nth (0 <= n <= size()-1) element of the CircularArray. |
E |
getFirst() Get first element of the CircularArray. |
E |
getLast() Get last element of the CircularArray. |
Boolean |
isEmpty() Return true if size() is 0. |
E |
popFirst() Remove first element from front of the CircularArray and return it. |
E |
popLast() Remove last element from end of the CircularArray and return it. |
Unit |
removeFromEnd(numOfElements: Int) Remove multiple elements from end of the CircularArray, ignore when numOfElements is less than or equals to 0. |
Unit |
removeFromStart(numOfElements: Int) Remove multiple elements from front of the CircularArray, ignore when numOfElements is less than or equals to 0. |
Int |
size() Get number of elements in the CircularArray. |
Public constructors
<init>
CircularArray()
Creates a circular array with default capacity.
<init>
CircularArray(minCapacity: Int)
Creates a circular array with capacity for at least minCapacity
elements.
Parameters | |
---|---|
minCapacity |
Int: the minimum capacity, between 1 and 2^30 inclusive |
Public methods
addFirst
fun addFirst(e: E): Unit
Add an element in front of the CircularArray.
Parameters | |
---|---|
e |
E: Element to add. |
addLast
fun addLast(e: E): Unit
Add an element at end of the CircularArray.
Parameters | |
---|---|
e |
E: Element to add. |
get
fun get(n: Int): E
Get nth (0 <= n <= size()-1) element of the CircularArray.
Parameters | |
---|---|
n |
Int: The zero based element index in the CircularArray. |
Return | |
---|---|
E: The nth element. |
Exceptions | |
---|---|
|
ArrayIndexOutOfBoundsException if n < 0 or n >= size(). |
getFirst
fun getFirst(): E
Get first element of the CircularArray.
Return | |
---|---|
E: The first element. |
Exceptions | |
---|---|
|
ArrayIndexOutOfBoundsException if CircularArray is empty. |
getLast
fun getLast(): E
Get last element of the CircularArray.
Return | |
---|---|
E: The last element. |
Exceptions | |
---|---|
|
ArrayIndexOutOfBoundsException if CircularArray is empty. |
popFirst
fun popFirst(): E
Remove first element from front of the CircularArray and return it.
Return | |
---|---|
E: The element removed. |
Exceptions | |
---|---|
ArrayIndexOutOfBoundsException |
if CircularArray is empty. |
popLast
fun popLast(): E
Remove last element from end of the CircularArray and return it.
Return | |
---|---|
E: The element removed. |
Exceptions | |
---|---|
ArrayIndexOutOfBoundsException |
if CircularArray is empty. |
removeFromEnd
fun removeFromEnd(numOfElements: Int): Unit
Remove multiple elements from end of the CircularArray, ignore when numOfElements is less than or equals to 0.
Parameters | |
---|---|
numOfElements |
Int: Number of elements to remove. |
Exceptions | |
---|---|
ArrayIndexOutOfBoundsException |
if numOfElements is larger than size() |
removeFromStart
fun removeFromStart(numOfElements: Int): Unit
Remove multiple elements from front of the CircularArray, ignore when numOfElements is less than or equals to 0.
Parameters | |
---|---|
numOfElements |
Int: Number of elements to remove. |
Exceptions | |
---|---|
ArrayIndexOutOfBoundsException |
if numOfElements is larger than size() |