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)) }

6th Oct 2018, 4:24 AM
Aiden
Aiden - avatar
1 Resposta
+ 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
7th Oct 2018, 4:41 PM
John Wells
John Wells - avatar