# CircularArray

Artifact: [androidx.collection:collection](https://developer.android.com/jetpack/androidx/releases/collection) [View Source](https://cs.android.com/search?q=file:androidx/collection/CircularArray.kt+class:androidx.collection.CircularArray) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

*** ** * ** ***


```
public final class CircularArray<E extends Object>
```

<br />

*** ** * ** ***

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 |
|---|
| `<E extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/collection/CircularArray#CircularArray(kotlin.Int)(int minCapacity)` Creates a circular array with capacity for at least `https://developer.android.com/reference/androidx/collection/CircularArray#(kotlin.Int)` elements. |

| ### Public methods |
|---|---|
| `final void` | `https://developer.android.com/reference/androidx/collection/CircularArray#addFirst(kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull E element)` Add an element in front of the `https://developer.android.com/reference/androidx/collection/CircularArray`. |
| `final void` | `https://developer.android.com/reference/androidx/collection/CircularArray#addLast(kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull E element)` Add an element at end of the CircularArray. |
| `final void` | `https://developer.android.com/reference/androidx/collection/CircularArray#clear()()` Remove all elements from the `https://developer.android.com/reference/androidx/collection/CircularArray`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull E` | `https://developer.android.com/reference/androidx/collection/CircularArray#get(kotlin.Int)(int index)` Get nth (0 \<= n \<= size()-1) element of the `https://developer.android.com/reference/androidx/collection/CircularArray`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull E` | `https://developer.android.com/reference/androidx/collection/CircularArray#getFirst()()` Get first element of the `https://developer.android.com/reference/androidx/collection/CircularArray`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull E` | `https://developer.android.com/reference/androidx/collection/CircularArray#getLast()()` Get last element of the `https://developer.android.com/reference/androidx/collection/CircularArray`. |
| `final boolean` | `https://developer.android.com/reference/androidx/collection/CircularArray#isEmpty()()` Return `true` if `https://developer.android.com/reference/androidx/collection/CircularArray#size()` is 0. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull E` | `https://developer.android.com/reference/androidx/collection/CircularArray#popFirst()()` Remove first element from front of the `https://developer.android.com/reference/androidx/collection/CircularArray` and return it. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull E` | `https://developer.android.com/reference/androidx/collection/CircularArray#popLast()()` Remove last element from end of the `https://developer.android.com/reference/androidx/collection/CircularArray` and return it. |
| `final void` | `https://developer.android.com/reference/androidx/collection/CircularArray#removeFromEnd(kotlin.Int)(int count)` Remove multiple elements from end of the `https://developer.android.com/reference/androidx/collection/CircularArray`, ignore when `https://developer.android.com/reference/androidx/collection/CircularArray#removeFromEnd(kotlin.Int)` is less than or equals to 0. |
| `final void` | `https://developer.android.com/reference/androidx/collection/CircularArray#removeFromStart(kotlin.Int)(int count)` Remove multiple elements from front of the `https://developer.android.com/reference/androidx/collection/CircularArray`, ignore when `https://developer.android.com/reference/androidx/collection/CircularArray#removeFromStart(kotlin.Int)` is less than or equal to 0. |
| `final int` | `https://developer.android.com/reference/androidx/collection/CircularArray#size()()` Get number of elements in the `https://developer.android.com/reference/androidx/collection/CircularArray`. |

## Public constructors

### CircularArray

```
public <E extends Object> CircularArray(int minCapacity)
```

Creates a circular array with capacity for at least `https://developer.android.com/reference/androidx/collection/CircularArray#(kotlin.Int)` elements.

| Parameters |
|---|---|
| `int minCapacity` | the minimum capacity, between 1 and 2\^30 inclusive |

## Public methods

### addFirst

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final void addFirst(@NonNull E element)
```

Add an element in front of the `https://developer.android.com/reference/androidx/collection/CircularArray`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull E element` | Element to add. |

### addLast

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final void addLast(@NonNull E element)
```

Add an element at end of the CircularArray.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull E element` | Element to add. |

### clear

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final void clear()
```

Remove all elements from the `https://developer.android.com/reference/androidx/collection/CircularArray`.

### get

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final @NonNull E get(int index)
```

Get nth (0 \<= n \<= size()-1) element of the `https://developer.android.com/reference/androidx/collection/CircularArray`.

| Parameters |
|---|---|
| `int index` | The zero based element index in the `https://developer.android.com/reference/androidx/collection/CircularArray`. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull E` | The nth element. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-index-out-of-bounds-exception/index.html` | if n \< 0 or n \>= size() |

### getFirst

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final @NonNull E getFirst()
```

Get first element of the `https://developer.android.com/reference/androidx/collection/CircularArray`.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull E` | The first element. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-index-out-of-bounds-exception/index.html` | if `https://developer.android.com/reference/androidx/collection/CircularArray` is empty |

### getLast

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final @NonNull E getLast()
```

Get last element of the `https://developer.android.com/reference/androidx/collection/CircularArray`.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull E` | The last element. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-index-out-of-bounds-exception/index.html` | if `https://developer.android.com/reference/androidx/collection/CircularArray` is empty |

### isEmpty

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final boolean isEmpty()
```

Return `true` if `https://developer.android.com/reference/androidx/collection/CircularArray#size()` is 0.

| Returns |
|---|---|
| `boolean` | `true` if `https://developer.android.com/reference/androidx/collection/CircularArray#size()` is 0. |

### popFirst

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final @NonNull E popFirst()
```

Remove first element from front of the `https://developer.android.com/reference/androidx/collection/CircularArray` and return it.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull E` | The element removed. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-index-out-of-bounds-exception/index.html` | if `https://developer.android.com/reference/androidx/collection/CircularArray` is empty (on jvm) |

### popLast

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final @NonNull E popLast()
```

Remove last element from end of the `https://developer.android.com/reference/androidx/collection/CircularArray` and return it.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull E` | The element removed. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-index-out-of-bounds-exception/index.html` | if `https://developer.android.com/reference/androidx/collection/CircularArray` is empty |

### removeFromEnd

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final void removeFromEnd(int count)
```

Remove multiple elements from end of the `https://developer.android.com/reference/androidx/collection/CircularArray`, ignore when `https://developer.android.com/reference/androidx/collection/CircularArray#removeFromEnd(kotlin.Int)` is less than or equals to 0.

| Parameters |
|---|---|
| `int count` | Number of elements to remove. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-index-out-of-bounds-exception/index.html` | if `https://developer.android.com/reference/androidx/collection/CircularArray#removeFromEnd(kotlin.Int)` is larger than `https://developer.android.com/reference/androidx/collection/CircularArray#size()` |

### removeFromStart

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final void removeFromStart(int count)
```

Remove multiple elements from front of the `https://developer.android.com/reference/androidx/collection/CircularArray`, ignore when `https://developer.android.com/reference/androidx/collection/CircularArray#removeFromStart(kotlin.Int)` is less than or equal to 0.

| Parameters |
|---|---|
| `int count` | Number of elements to remove. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-index-out-of-bounds-exception/index.html` | if `https://developer.android.com/reference/androidx/collection/CircularArray#removeFromStart(kotlin.Int)` is larger than `https://developer.android.com/reference/androidx/collection/CircularArray#size()` |

### size

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

```
public final int size()
```

Get number of elements in the `https://developer.android.com/reference/androidx/collection/CircularArray`.

| Returns |
|---|---|
| `int` | Number of elements in the `https://developer.android.com/reference/androidx/collection/CircularArray`. |