how to replace elements in array or array list by index in kotlin | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

how to replace elements in array or array list by index in kotlin

how to replace elements in array or array list by index in kotlin

17th Jan 2023, 3:25 PM
mostafa nady
mostafa nady - avatar
5 Answers
+ 3
You can do it in a few ways using loops. Do you have a code that doesn't work? Please attach it and someone can better help you.
17th Jan 2023, 4:47 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
You can always check the documentation if you are unsure how to use a language feature. https://kotlinlang.org/docs/arrays.html In Kotlin generally we prefer to use immutable collections, and we manipulate values by using any of the built-in methods such as map, filter and reduce, and a few dozen others.
17th Jan 2023, 7:41 PM
Tibor Santa
Tibor Santa - avatar
+ 1
i want to replace an element in array by another element in the same array ,not to add a new element
17th Jan 2023, 5:33 PM
mostafa nady
mostafa nady - avatar
+ 1
fun main(args: Array<String>) { var num1: Int = 0 var arraylist = Array<Int>(100) { 0 } var arrof = arrayOf(*arraylist) println("enter num of tests ") var num_of_test = readln()!!.toInt() for (i in 1..num_of_test step 1) { println("enter number of num ") num1 = readLine()!!.toInt() for (i in 0..num1 - 1 step 1) { arrof[i] = readLine()!!.toInt() } for (i in 0..num1-1){ if (arrof[i + 2] == arrof[i + 1] + arrof[i]) { arrof.set(i, arrof[i + 2]) arrof.set(i+2,arrof[i]) } } for (i in 0..num1 - 1) { println(arrof[i]) } } }
17th Jan 2023, 5:36 PM
mostafa nady
mostafa nady - avatar
+ 1
thanks a lot
17th Jan 2023, 6:44 PM
mostafa nady
mostafa nady - avatar