Having problem with ```toTypedArray```

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 :slight_smile:

1 Like

And what is your problem?

(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.)

1 Like

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.

1 Like
fun <T> correctStream(user: Array<T>, correct: Array<T>): IntArray =
		.map { if (it.first == it.second) 1 else -1 }
1 Like

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.

Well, either way the zip() function seems about right for this task.

1 Like

hmm :confused: it appears I have problem with Data Structures in Kotlin. List, Array, IntArray… lots of names here :expressionless:
@gidds also mention that above, thanks :slight_smile:

I am going to try it, thanks :slight_smile:

1 Like
  • 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 List you 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.

1 Like

Hi, @tieskedh. :slight_smile: thanks. :slight_smile:
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. :smiley:

1 Like

Hi ! If you work with fixed length you can directly build the array result.

fun correctSteam(user: Array<String>, correct: Array<String>) = IntArray(user.size) { index ->
    if(user[index] == correct[index]) 1 else -1
1 Like

Thank you :slight_smile:
I just tried your way, but it till doesn’t give me what I need.

    arrayOf("cat", "blau", "skt", "umbrells", "paddy"),
     arrayOf("cat", "blue", "sky", "umbrella", "paddy")))

try it with that.

what I got, was this [I@337d0578

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?? :thinking:


for (item in correctStream(arrayOf("cat", "blau", "skt", "umbrells", "paddy"), arrayOf("cat", "blue", "sky", "umbrella", "paddy"))) {
         println(Exception().stackTrace[0].lineNumber.toString() + " / " + Exception().stackTrace[0]?.fileName.toString() + " " + item)

don’t get shocked by that ling println, I just put those lineNumber an fileName there, as a habit, to easily keep tract of the printed statements. :slight_smile:

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().

I get you an example at https://pl.kotl.in/DCdA9dwPX.

1 Like

Thank you.

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 :laughing:

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.

1 Like