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

Creates a circular array with default capacity.

<init>(minCapacity: Int)

Creates a circular array with capacity for at least minCapacity elements.

Public methods
Unit

Remove all elements from the CircularArray.

E

Get first element of the CircularArray.

Boolean

Return true if size() is 0.

Unit
removeFromStart(numOfElements: Int)

Remove multiple elements from front of the CircularArray, ignore when numOfElements is less than or equals to 0.

E

Get last element of the CircularArray.

Unit
addLast(e: E)

Add an element at end of the CircularArray.

E

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
addFirst(e: E)

Add an element in front of the CircularArray.

Int

Get number of elements in the CircularArray.

E
get(n: Int)

Get nth (0 <= n <= size()-1) element of the CircularArray.

E

Remove first element from front of the CircularArray and return it.

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

clear

fun clear(): Unit

Remove all elements from the CircularArray.

getFirst

fun getFirst(): E

Get first element of the CircularArray.

Return
E: The first element.
Exceptions
ArrayIndexOutOfBoundsException if CircularArray is empty.

isEmpty

fun isEmpty(): Boolean

Return true if size() is 0.

Return
Boolean: true if size() is 0.

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()

getLast

fun getLast(): E

Get last element of the CircularArray.

Return
E: The last element.
Exceptions
ArrayIndexOutOfBoundsException if CircularArray is empty.

addLast

fun addLast(e: E): Unit

Add an element at end of the CircularArray.

Parameters
e E: Element to add.

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()

addFirst

fun addFirst(e: E): Unit

Add an element in front of the CircularArray.

Parameters
e E: Element to add.

size

fun size(): Int

Get number of elements in the CircularArray.

Return
Int: Number of elements in the CircularArray.

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().

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.