Niklas Flink
Niklas Flink

Reputation: 101

"Arrays.copyOf" in Kotlin

I've got a quick question about the function "Arrays.copyOf()". I have an IntArray "rows" and an Arraylist "listMoves". I want to create a copy of rows and add it to listMoves like this:

var rows: IntArray

val listMoves: MutableList<IntArray> = arrayListOf() 

listMoves.add(Arrays.copyOf(rows, rows.size))

Is there a function in kotlin that is equivalent to "Arrays.copyOf()" and how should I use it? The main problem is, that I am not allowed to use any library from Java (Need to use "import java.util.*" here). Thanks in advance.

Upvotes: 5

Views: 3261

Answers (1)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20646

You can use copyOf which will return :

Returns new array which is a copy of the original array, resized to the given newSize. The copy is either truncated or padded at the end with null values if necessary.

You can use it as follows :

val yourNewArray = arrayToCopy.copyOf()

Upvotes: 7

Related Questions