Homework: SimpleArrayList add

class SimpleArrayList(given: Array<Any?>) {
private var values: Array<Any?> = given
fun size(): Int {
return values.size
}
fun getValues(): Array<Any?> {
return values
}
fun add(index: Int, value: Any?) {
require(index <= values.size && index >= 0)
var newArray = values.slice(0…index - 1) + value + values.slice(index…values.size - 1)
values = arrayOf(newArray)
}
}
I dont know how else I can solve this question. I never used the java library but Ive been getting java errors

The output of your code is not the expected output, and try to use for loop to solve this question.

class SimpleArrayList(given: Array<Any?>) {
  private var values: Array<Any?> = given
  fun size(): Int {
    return values.size
  }
  fun getValues(): Array<Any?> {
    return values
  }
  fun add(index: Int, value: Any?) {
    require(index <= values.size && index >= 0)
    var newArray = values.slice(0..index - 1) + value + values.slice(index..values.size - 1)
    values = arrayOf(newArray)
  }
}
var test = SimpleArrayList(Array<Any?>(0) { null })
test.add(0, 1)
test.add(0, 2)
println(test.getValues().contentToString()) // the output should be [2, 1]