0

I would like to know what is the difference between array (arrayOf), list (listOf), and arraylist (arrayListOf) in Kotlin? and how to use them in appropriate way? Thank you.

Beby
  • 29
  • 4
  • https://stackoverflow.com/questions/60428161/using-listof-vs-arraylistof – ADM Apr 19 '23 at 05:32
  • This was closed as a duplicate of the wrong question by the Community bot. The above commented question link is related but not exactly what this person is asking. – Tenfour04 Apr 19 '23 at 07:23

3 Answers3

2

An Array is a fixed-size container. You cannot add or remove items from it--only replace them. You rarely need to use arrayOf or create your own Array unless some function you are interacting with requires it, or you are working with two(or more)-dimensional, fixed-size data.


A MutableList is variable-size container. Adding and removing items resizes it automatically.

ArrayList is a specific type of MutableList, but there are several other available implementations of MutableList, such as ArrayDeque, LinkedList, and Vector, each with different pros and cons.

Usually, you only need to use MutableList and mutableListOf() rather than a specific type. But arrayListOf() is available as a convenient way to create an ArrayList in cases where you specifically need that type.


List is a read-only container. This means you cannot add or remove from it. If you use listOf(), it creates an immutable List, but if some other piece of code passes you a List, it might actually be a read-only view of a MutableList, so you don't automatically have a guarantee that it is immutable. A MutableList is also a List and can therefore be upcast to a List.


Out of all of these List should usually be your default choice. It is less error-prone to work with lists that don't mutate. If you put a List in a var property, you can still mutate the property.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • OP asked about arrayListOf() not mutableListOf() – m0skit0 Apr 19 '23 at 07:58
  • 1
    I think you have to understand what MutableList is before ArrayList. Third paragraph talks about ArrayList. I added a sentence about `arrayListOf`. – Tenfour04 Apr 19 '23 at 08:00
  • The problem is your phrase "in cases where you specifically need that type", because that's what the OP was asking, "when/why would I specifically need that type?" The answer at https://stackoverflow.com/a/66915258/7386018 seems to give a concrete answer to the question – Markers Jul 09 '23 at 11:38
0

Arrays have fixed length but are mutable

array = arrayOf(0, 1, 2, 3)
array[2] = 1 //fine, array = 0, 1, 1, 3

Lists are immutable(except mutable lists)

list = listOf(0, 1, 2, 3)
list[2] = 1 //error thrown

Array lists have lengths that can be modified. Elements can be added at the end of the list.

arrayList = arrayListOf(0, 1, 2, 3)
arrayList.add(4) // arrayList = 0, 1, 2, 3, 4

Or you can add to a specific index

arrayList.add(1 /* The index*/, 3 /* The value of the element*/)
//arrayList is 0, 3, 1, 2, 3, 4

You can also remove elements, using the value:

arrayList.remove(3) //This will remove the first 3 it founds in the list, arrayList = 0, 1, 2, 3, 4

Or you could remove elements using the index

arrayList.removeAt(4) //This will remove the element with index 4, arrayList = 0, 1, 2, 3
danny
  • 37
  • 7
0

Array is the equivalent of Java []. It's not even a list, it's a very old low level Java thing, relic of the past, before generics. You don't use it at all, unless you have to.

List is the most generic, readonly list interface. Use it when you don't know and you don't care about the underlying implementation. This is what you're going to use most of the time. Most of the reasonably new code operates on List. Basically, you always use this unless there's a good reason to not to.

ArrayList is a particular implementation of List. Just as #1: don't use it unless you have to. Contrary to the name, it has nothing to do with Array.

There's a another one that you should know: MutableList (mutableListOf()). It returns a MutableList, a superset of List. MutableList is also abstract, like List, but it can also add and remove elements. Currently, mutableListOf actually returns ArrayList. But the best part is: even if that ever changes - it won't bother you. You use it when you need a list that can be modified (mutated). But over they last decades we have learned that mutability is actually bad and should be avoided.

In 99% "you have to" in points #1 and #3 means there is some legacy code that accepts input as something other than List.

Also, don't worry too much upfront. You can always do everything on the most abstract List and add .toArray() or .toArrayList() or .toMutableList() at the very end, if you have to.

So the bottom line is: Use List. If you can't work with immutable list, use MutableList. If the code you're working with can't handle one of those two, it's a shitty code and you compromise with .toWhateverTheyCanWorkWith().

Agent_L
  • 4,960
  • 28
  • 30