# CircularArray

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  
Summary:[Ctors](https://developer.android.com/reference/android/support/v4/util/CircularArray#pubctors)\|[Methods](https://developer.android.com/reference/android/support/v4/util/CircularArray#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/v4/util/CircularArray#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/v4/util/CircularArray#)  

# CircularArray

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public

final

class
CircularArray
`  
`

extends Object


``


`

|---|--------------------------------------------|
| java.lang.Object                              ||
| ↳ | android.support.v4.util.CircularArray\<E\> |

<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                                                                                                ||
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[CircularArray](https://developer.android.com/reference/android/support/v4/util/CircularArray#CircularArray())`() ` Creates a circular array with default capacity.                                             |
| ` `[CircularArray](https://developer.android.com/reference/android/support/v4/util/CircularArray#CircularArray(int))`(int minCapacity) ` Creates a circular array with capacity for at least`minCapacity`elements. |

|                                                                                                                             ### Public methods                                                                                                                              ||
|------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void`    | ` `[addFirst](https://developer.android.com/reference/android/support/v4/util/CircularArray#addFirst(E))`(E e) ` Add an element in front of the CircularArray.                                                                                                  |
| ` void`    | ` `[addLast](https://developer.android.com/reference/android/support/v4/util/CircularArray#addLast(E))`(E e) ` Add an element at end of the CircularArray.                                                                                                      |
| ` void`    | ` `[clear](https://developer.android.com/reference/android/support/v4/util/CircularArray#clear())`() ` Remove all elements from the CircularArray.                                                                                                              |
| ` E`       | ` `[get](https://developer.android.com/reference/android/support/v4/util/CircularArray#get(int))`(int n) ` Get nth (0 \<= n \<= size()-1) element of the CircularArray.                                                                                         |
| ` E`       | ` `[getFirst](https://developer.android.com/reference/android/support/v4/util/CircularArray#getFirst())`() ` Get first element of the CircularArray.                                                                                                            |
| ` E`       | ` `[getLast](https://developer.android.com/reference/android/support/v4/util/CircularArray#getLast())`() ` Get last element of the CircularArray.                                                                                                               |
| ` boolean` | ` `[isEmpty](https://developer.android.com/reference/android/support/v4/util/CircularArray#isEmpty())`() ` Return true if size() is 0.                                                                                                                          |
| ` E`       | ` `[popFirst](https://developer.android.com/reference/android/support/v4/util/CircularArray#popFirst())`() ` Remove first element from front of the CircularArray and return it.                                                                                |
| ` E`       | ` `[popLast](https://developer.android.com/reference/android/support/v4/util/CircularArray#popLast())`() ` Remove last element from end of the CircularArray and return it.                                                                                     |
| ` void`    | ` `[removeFromEnd](https://developer.android.com/reference/android/support/v4/util/CircularArray#removeFromEnd(int))`(int numOfElements) ` Remove multiple elements from end of the CircularArray, ignore when numOfElements is less than or equals to 0.       |
| ` void`    | ` `[removeFromStart](https://developer.android.com/reference/android/support/v4/util/CircularArray#removeFromStart(int))`(int numOfElements) ` Remove multiple elements from front of the CircularArray, ignore when numOfElements is less than or equals to 0. |
| ` int`     | ` `[size](https://developer.android.com/reference/android/support/v4/util/CircularArray#size())`() ` Get number of elements in the CircularArray.                                                                                                               |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v4/util/CircularArray#)From class` java.lang.Object ` |-------------------|-------------------------------| | ` Object`         | ` clone() `                   | | ` boolean`        | ` equals(Object arg0) `       | | ` void`           | ` finalize() `                | | ` final Class<?>` | ` getClass() `                | | ` int`            | ` hashCode() `                | | ` final void`     | ` notify() `                  | | ` final void`     | ` notifyAll() `               | | ` String`         | ` toString() `                | | ` final void`     | ` wait(long arg0, int arg1) ` | | ` final void`     | ` wait(long arg0) `           | | ` final void`     | ` wait() `                    | ||

## Public constructors

### CircularArray

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
CircularArray ()
```

Creates a circular array with default capacity.

<br />

### CircularArray

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
CircularArray (int minCapacity)
```

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

<br />

|                                    Parameters                                    ||
|---------------|-------------------------------------------------------------------|
| `minCapacity` | `int`: the minimum capacity, between 1 and 2\^30 inclusive <br /> |

## Public methods

### addFirst

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void addFirst (E e)
```

Add an element in front of the CircularArray.

<br />

|            Parameters            ||
|-----|-----------------------------|
| `e` | `E`: Element to add. <br /> |

### addLast

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void addLast (E e)
```

Add an element at end of the CircularArray.

<br />

|            Parameters            ||
|-----|-----------------------------|
| `e` | `E`: Element to add. <br /> |

### clear

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void clear ()
```

Remove all elements from the CircularArray.

<br />

### get

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
E get (int n)
```

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

<br />

|                              Parameters                               ||
|-----|------------------------------------------------------------------|
| `n` | `int`: The zero based element index in the CircularArray. <br /> |

|           Returns            ||
|-----|-------------------------|
| `E` | The nth element. <br /> |

|                            Throws                             ||
|---|------------------------------------------------------------|
|   | ArrayIndexOutOfBoundsException} if n \< 0 or n \>= size(). |

### getFirst

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
E getFirst ()
```

Get first element of the CircularArray.

<br />

|            Returns             ||
|-----|---------------------------|
| `E` | The first element. <br /> |

|                            Throws                             ||
|---|------------------------------------------------------------|
|   | ArrayIndexOutOfBoundsException} if CircularArray is empty. |

### getLast

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
E getLast ()
```

Get last element of the CircularArray.

<br />

|            Returns            ||
|-----|--------------------------|
| `E` | The last element. <br /> |

|                            Throws                             ||
|---|------------------------------------------------------------|
|   | ArrayIndexOutOfBoundsException} if CircularArray is empty. |

### isEmpty

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
boolean isEmpty ()
```

Return true if size() is 0.

<br />

|                Returns                 ||
|-----------|-----------------------------|
| `boolean` | true if size() is 0. <br /> |

### popFirst

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
E popFirst ()
```

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

<br />

|             Returns              ||
|-----|-----------------------------|
| `E` | The element removed. <br /> |

|                            Throws                            ||
|----------------------------------|----------------------------|
| `ArrayIndexOutOfBoundsException` | if CircularArray is empty. |

### popLast

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
E popLast ()
```

Remove last element from end of the CircularArray and return it.

<br />

|             Returns              ||
|-----|-----------------------------|
| `E` | The element removed. <br /> |

|                            Throws                            ||
|----------------------------------|----------------------------|
| `ArrayIndexOutOfBoundsException` | if CircularArray is empty. |

### removeFromEnd

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void removeFromEnd (int numOfElements)
```

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

<br />

|                          Parameters                          ||
|-----------------|---------------------------------------------|
| `numOfElements` | `int`: Number of elements to remove. <br /> |

|                                                                             Throws                                                                              ||
|----------------------------------|-------------------------------------------------------------------------------------------------------------------------------|
| `ArrayIndexOutOfBoundsException` | if numOfElements is larger than[size()](https://developer.android.com/reference/android/support/v4/util/CircularArray#size()) |

### removeFromStart

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void removeFromStart (int numOfElements)
```

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

<br />

|                          Parameters                          ||
|-----------------|---------------------------------------------|
| `numOfElements` | `int`: Number of elements to remove. <br /> |

|                                                                             Throws                                                                              ||
|----------------------------------|-------------------------------------------------------------------------------------------------------------------------------|
| `ArrayIndexOutOfBoundsException` | if numOfElements is larger than[size()](https://developer.android.com/reference/android/support/v4/util/CircularArray#size()) |

### size

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int size ()
```

Get number of elements in the CircularArray.

<br />

|                        Returns                         ||
|-------|-------------------------------------------------|
| `int` | Number of elements in the CircularArray. <br /> |

-

  ## Interfaces

  - [Pools.Pool](https://developer.android.com/reference/android/support/v4/util/Pools.Pool)
-

  ## Classes

  - [ArrayMap](https://developer.android.com/reference/android/support/v4/util/ArrayMap)
  - [ArraySet](https://developer.android.com/reference/android/support/v4/util/ArraySet)
  - [AtomicFile](https://developer.android.com/reference/android/support/v4/util/AtomicFile)
  - [CircularArray](https://developer.android.com/reference/android/support/v4/util/CircularArray)
  - [CircularIntArray](https://developer.android.com/reference/android/support/v4/util/CircularIntArray)
  - [LongSparseArray](https://developer.android.com/reference/android/support/v4/util/LongSparseArray)
  - [LruCache](https://developer.android.com/reference/android/support/v4/util/LruCache)
  - [ObjectsCompat](https://developer.android.com/reference/android/support/v4/util/ObjectsCompat)
  - [Pair](https://developer.android.com/reference/android/support/v4/util/Pair)
  - [PatternsCompat](https://developer.android.com/reference/android/support/v4/util/PatternsCompat)
  - [Pools](https://developer.android.com/reference/android/support/v4/util/Pools)
  - [Pools.SimplePool](https://developer.android.com/reference/android/support/v4/util/Pools.SimplePool)
  - [Pools.SynchronizedPool](https://developer.android.com/reference/android/support/v4/util/Pools.SynchronizedPool)
  - [SimpleArrayMap](https://developer.android.com/reference/android/support/v4/util/SimpleArrayMap)
  - [SparseArrayCompat](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat)