# CircularArray

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

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/collection/CircularArray "View this page in Java")
Common/AllAndroid/JVMNative/C/iOSJavaScript
  - Cmn  

  ```
  class CircularArray<E : Any?>
  ```

*** ** * ** ***

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 : `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?> `[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#CircularArray(kotlin.Int))`(minCapacity: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Creates a circular array with capacity for at least[minCapacity](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#<init>(kotlin.Int))elements. | Cmn |

|                                ### Public functions                                 |
|-------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [addFirst](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#addFirst(kotlin.Any))`(element: E)` Add an element in front of the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray).                                                                                                                                                                                                                                                                 | Cmn |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [addLast](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#addLast(kotlin.Any))`(element: E)` Add an element at end of the CircularArray.                                                                                                                                                                                                                                                                                                                                                        | Cmn |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [clear](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#clear())`()` Remove all elements from the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray).                                                                                                                                                                                                                                                                                             | Cmn |
| `operator E`                                                                        | [get](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#get(kotlin.Int))`(index: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Get nth (0 \<= n \<= size()-1) element of the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray).                                                                                                                                                                                  | Cmn |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [isEmpty](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#isEmpty())`()` Return`true`if[size](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#size())is 0.                                                                                                                                                                                                                                                                                                     | Cmn |
| `E`                                                                                 | [popFirst](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#popFirst())`()` Remove first element from front of the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray)and return it.                                                                                                                                                                                                                                                                | Cmn |
| `E`                                                                                 | [popLast](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#popLast())`()` Remove last element from end of the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray)and return it.                                                                                                                                                                                                                                                                     | Cmn |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [removeFromEnd](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#removeFromEnd(kotlin.Int))`(count: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Remove multiple elements from end of the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray), ignore when[count](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#removeFromEnd(kotlin.Int))is less than or equals to 0.        | Cmn |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [removeFromStart](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#removeFromStart(kotlin.Int))`(count: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Remove multiple elements from front of the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray), ignore when[count](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#removeFromStart(kotlin.Int))is less than or equal to 0. | Cmn |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)         | [size](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#size())`()` Get number of elements in the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray).                                                                                                                                                                                                                                                                                              | Cmn |

| ### Public properties |
|-----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|
| `E`                   | [first](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#first()) Get first element of the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray). | Cmn |
| `E`                   | [last](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#last()) Get last element of the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray).    | Cmn |

## Public constructors

### CircularArray

Cmn  

```
<E : Any?> CircularArray(minCapacity: Int = 8)
```

Creates a circular array with capacity for at least[minCapacity](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#<init>(kotlin.Int))elements.  

|                                            Parameters                                            |
|--------------------------------------------------------------------------------------------------|-----------------------------------------------------|
| `minCapacity: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` = 8` | the minimum capacity, between 1 and 2\^30 inclusive |

## Public functions

### addFirst

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

```
fun addFirst(element: E): Unit
```

Add an element in front of the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray).  

|  Parameters  |
|--------------|-----------------|
| `element: E` | Element to add. |

### addLast

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

```
fun addLast(element: E): Unit
```

Add an element at end of the CircularArray.  

|  Parameters  |
|--------------|-----------------|
| `element: E` | Element to add. |

### clear

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

```
fun clear(): Unit
```

Remove all elements from the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray).  

### get

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

```
operator fun get(index: Int): E
```

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

|                                      Parameters                                      |
|--------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|
| `index: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The zero based element index in the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray). |

| Returns |
|---------|------------------|
| `E`     | The nth element. |

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

### isEmpty

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

```
fun isEmpty(): Boolean
```

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

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | `true`if[size](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray#size())is 0. |

### popFirst

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

```
fun popFirst(): E
```

Remove first element from front of the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray)and return it.  

| Returns |
|---------|----------------------|
| `E`     | The element removed. |

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

### popLast

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

```
fun popLast(): E
```

Remove last element from end of the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray)and return it.  

| Returns |
|---------|----------------------|
| `E`     | The element removed. |

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

### removeFromEnd

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

```
fun removeFromEnd(count: Int): Unit
```

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

|                                      Parameters                                      |
|--------------------------------------------------------------------------------------|-------------------------------|
| `count: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Number of elements to remove. |

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

### removeFromStart

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

```
fun removeFromStart(count: Int): Unit
```

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

|                                      Parameters                                      |
|--------------------------------------------------------------------------------------|-------------------------------|
| `count: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Number of elements to remove. |

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

### size

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

```
fun size(): Int
```

Get number of elements in the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray).  

|                                   Returns                                   |
|-----------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Number of elements in the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray). |

## Public properties

### first

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

```
val first: E
```

Get first element of the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray).  

| Returns |
|---------|--------------------|
| `E`     | The first element. |

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

### last

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

```
val last: E
```

Get last element of the[CircularArray](https://developer.android.com/reference/kotlin/androidx/collection/CircularArray).  

| Returns |
|---------|-------------------|
| `E`     | The last element. |

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