+ 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
7 odpowiedzi
+ 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.
+ 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.
+ 1
i want to replace an element in array by another element in the same array ,not to add a new element
+ 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])
}
}
}
+ 1
thanks a lot