# Should Array have a getOrPut method?

**URL:** <https://discuss.kotlinlang.org/t/should-array-have-a-getorput-method/28791>\
**Category:** Libraries\
**Created:** [June 23, 2024, 4:38pm UTC](https://discuss.kotlinlang.org/t/should-array-have-a-getorput-method/28791 "2024-06-23T16:38:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian.dastous](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@brian.dastous](https://discuss.kotlinlang.org/u/brian.dastous)\
**Post date:** [June 23, 2024, 4:38pm UTC](https://discuss.kotlinlang.org/t/should-array-have-a-getorput-method/28791/1 "2024-06-23T16:38:11Z")

</div>

Arrays are often used as maps in the case where you have a relatively small number of integer keys. It seems like it would be useful to have a `getOrPut` method similar to that available for maps. The only interesting nuance I can think of here is that arrays have index bounds to worry about. So beyond a `getOrPut` that throws for invalid indices, maybe we would also want a `getOrNullOrPut` (name suggestions welcome…) that returns null for invalid indices.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [June 23, 2024, 5:10pm UTC](https://discuss.kotlinlang.org/t/should-array-have-a-getorput-method/28791/2 "2024-06-23T17:10:05Z")

</div>

I’m not exactly sure what is your expected behavior here. Arrays don’t have a concept of not existing items, so they always return a value. They also can’t “put” new items. Array is a collection of say 5 items, all of them exist and can be replaced by another value. This is pretty much it.

---

<div class="post-metadata">

**Author:** ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)\
**Post date:** [June 23, 2024, 11:39pm UTC](https://discuss.kotlinlang.org/t/should-array-have-a-getorput-method/28791/3 "2024-06-23T23:39:07Z")

</div>

Also, arrays aren’t used much in general Kotlin programming.

They’re needed for low-level implementation, for interoperability with Java and libraries, and can be used if you have really tight memory requirements. But they’re inferior to Lists in many ways: they don’t work well with generics (e.g. you can’t create an array of a generic type), they can’t be resized, the standard library provides less support for them, they have only a single implementation which can’t be overridden so no way to customise the performance or provide a decent toString() method, no support for multi-threading…

If you don’t have any of the speciaist requirements above, it’s usually better to use a List instead. (You can always create an explicit ArrayList if you need the specific behaviour and performance of an array, though it’s almost always better to use a List reference thereafter, just in case.)

---

<div class="post-metadata">

**Author:** ![TC](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@TC](https://discuss.kotlinlang.org/u/TC)\
**Post date:** [June 27, 2024, 6:47am UTC](https://discuss.kotlinlang.org/t/should-array-have-a-getorput-method/28791/4 "2024-06-27T06:47:01Z")

</div>

> [@brian.dastous](#):
>
> Arrays are often used as maps

This is a problem, not the lack of getOrPut.
