In Kotlin, how do I copy an ArrayList so changes to copy do not affect original?
11:35 29 Aug 2026

I'm new to kotlin, but I'm seeing some very surprising effects when using ArrayLists. In particular after copying a list using listCopy = listOriginal changes to listCopy also affect listOriginal.

I've seen this after performing various operations on the copied list.

As a fuller example:

        val listOriginal = ArrayList()
        listOriginal.add(5)
        listOriginal.add(8)
        listOriginal.add(3)
        val listCopy = listOriginal
        listCopy.add(7)
        listCopy.sort()

Following this both lists contain (3, 5, 7, 8), not just the copied one.

Can someone please explain why this is and how I actually copy a variable without having it linked to the original? I'm new to kotlin, but have used numerous other languages and not seen this behaviour before.

Also what other variable types have this sort of behaviour in kotlin?

android kotlin