0
Why can't I assign every element from user to arrayList in Kotlin?
var length: Int = readLine()!!.toInt() var actualLength: Int = length-1 var newArr = ArrayList<Int>(length) var x: Int = 0 while(x<length-1){ print("Enter element at $x: ") var temp: Int = readLine()!!.toInt() newArr.set(x, temp) x = x + 1 } println() for (element: Int in 0..actualLength){ println("Element at $element: "+newArr.get(element)) }
1 ответ
+ 1
Your newArr doesn't get any elements by default so you must add them yourself. Your loop fails to read the last element. Updated your code to work.
https://code.sololearn.com/c0hOqPw6h9pF