https://kotlinlang.org logo
#announcements
Title
# announcements
r

Roberto Messina

09/23/2019, 9:10 AM
There's any problem with reverseArray because copiler write that: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at my.demo.AppKt.invertivett(App.kt:58) at my.demo.AppKt.main(App.kt:47)
g

gildor

09/23/2019, 9:12 AM
Why do you need own reverseArray function if stdlib has .reverse() extension
r

Roberto Messina

09/23/2019, 9:18 AM
i didn't know that function 🙂 But, now i'm doing all the code without exstension to learn sintax and languages... Another question, where can i find kotlin's extensions?
g

gildor

09/23/2019, 9:59 AM
without exstension to learn sintax and languages
Isn’t stdlib functions is part of language and syntax?
☝️ 1
i find kotlin’s extensions?
What do you mean? Extension function is just a language feature: https://kotlinlang.org/docs/reference/extensions.html Standard library contains a lot of them, just check reference. For example this is doc for package
kotlin.collections
which contains a lot of extensions for different types, including `Array.reverse()`: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/index.html
also instead of
printArray
or
printStringArray
you can just use array extension
*Array.contentToString()
I think trying to reimplement all those function is not very efficient way, because instead using very practical and useful standard library to solve some tasks (even tasks just for education or samples) you spending time on implementing what already is part of the language (which also mean that you are not learning stdlib which especially important when you work on some real task in future)
@Roberto Messina Anyway, I also checked your
reverseArray
implementation and it’s expected that it’s not working, you use
for
loop in a wrong way. Not clearly sure what you try to achieve, you probably need range, because your code just iterate array with index and
var nMiddle
just not used (because one in for loop doesn’t override it). And use
withIndex
is not very efficcient way to work with arrays, it creates a lot of overhead (one additional object on every item) for case which can be easily solved without it But maybe you just need range, or something like this:
Copy code
for (i in (0..nMiddle))
2 Views