Home > Blockchain >  Why does 'indexOf' not find the specified arrayOfLong?
Why does 'indexOf' not find the specified arrayOfLong?

Time:04-03

I have an ArrayList of LongArrays, each of size 2. I am trying to use the built-in 'indexOf' method to find the index of a specific array of longs, and I can't figure out why the code says the array of longs I'm searching for isn't found. See the below screen shot of a debugging session where try to evaluate finding 'longArrayOf(0L,5L)' in the ArrayList. In my mind, longArrayOf(0L,5L) is clearly the first element in the cardHierarchy array. Can the 'indexOf' function not be used for finding arrays? Can anyone suggest an alternate method?

enter image description here

CodePudding user response:

indexOf uses Object.equals() when you pass arrays, which compares by reference address which is different for the LongArray passed to indexOf and the one present in cardHierarchy.

Change it to

cardHierarchy.indexOfFirst { it.contentEquals(longArrayOf(0L, 5L)) }
  • Related