having this function which takes in two array of strings, which they need to be compared. if the element at the same index are equal (same), return 1, otherwise -1
fun correctStream(user: Array<String>, correct: Array<String>): Array<Int> {
return IntRange(0, user.size - 1).map{ I: Int -> if (user[I] == correct[I]) 1 else -1}.toTypedArray
}
// it returns this [Ljava.lang.Integer;@51efea79
P.S. sure I can loop over one of the arrays and compare each element to the other one. but I’d like to use what Kotlin has offered, its features
(Other than failing to handle the case where the two arrays are of different sizes. Or using ‘Stream’ in a way that’s confusing for anyone who knows Java streams.)
You could simplify that slightly using zip(), but either way, if you really want to end up with an Array*, then toTypedArray will do fine.
(* Personally, I’d stick with Lists: they’re more flexible, and better supported.)
My guess is that you want it to return an IntArray insetad of Array<Int> to avoid boxing of integers. If I get the jvm notation right you want [I instead of [Ljava.lang.Integer.
To do so you need to use toIntArray() instead of toTypedArray.
my bad, I should’ve said that the arrays are same size.
I am quite new to Kotlin, and Java either. I am trying to solve some challenges that are presented in Java, I managed to solve this challenge in Java, using IntSteam, my aim is to solve the challenges in Kotlin by myself, there’s no test cases in Kotlin to check my solution against, but having it run in the editor with a given input, I think that’s enough for now.
Array: the most low level way to store a fixed number of values. This is used for high performance. I never use it.
List: stores numbers for you as well. But this is just an interface. It has multiple implementations for example:
good in writing or good in reading
can change /cannot change
does something on change
and many more, but unless you know you need something special, you can just use listOf/mutableListOf.
The difference between list and mutableList is that where you have a Listyou can change it, but if you have a MutableList you cannot change it. A List can be a mutableList, so it also tells you if you are supposed to change a List. Therefor, if you need to change a list, use a MutableList else use a List.
For returning from a function, almost always return a List. When you need to return a MutableList, you know it, because it would be hard/ugly to do it an other way.
Hi, @tieskedh. thanks.
you know, there are whole lots of names, that confused me. yes, I know, the Java Array is just like the Array that C has, Kotlin came up with a new Data Type that is kinda easier to work with, just like how Objective-C came up with NSArray. it’s got mutable ones which you can mutate it’s elements at anytime. it is the same with Kotlin, it’s just it is confusing sometimes, I think I will get use to it, I love it though.
wait!
What just happened? I looped over the result and got all the 1s one minus 1s.
but why?
why can’t I have the result of that function printed out to console just with a println() function??
Ok, I understand your problem. When you use the println function it implicitly call println(argument: Any) = println(argument.toString()). The fact is Array.toString() method only return the pointer hash of the Array. If you want to get the content of an array you should use Array.contentToString().
haaaa, I get it.
so, the Array.contentToString() solved the problem! finally!
but that’s still a work around to see the result. I think I should stick to the List data type as others suggested above.
I wonder why don’t they remove the Array data type entirely from the core since it is so messy
Array represents the lower level data structure. As you said above Array is like the C array and because kotlin is running on the JVM it is limited to the functionality of the jvm array.
Most of the time you should use List instead of Array but if you build your own data structures Array is useful since you can create more optimized code. Arrays provide the necessary low level functionality to build more powerful data structures.
And they are needed to interface with java code that uses arrays. Java interop is still one of the core design features of kotlin.