Kotlin 查找数组中对象的索引而不是对象本身
我有一个对象数组(称为Image的对象),我想在数组中找到符合特定条件的对象索引。相反,我当前的代码返回对象本身,而不是数组中的索引:Kotlin 查找数组中对象的索引而不是对象本身,kotlin,Kotlin,我有一个对象数组(称为Image的对象),我想在数组中找到符合特定条件的对象索引。相反,我当前的代码返回对象本身,而不是数组中的索引: val originalImage = imagesArray.find {it.position == 2} 我的图像类: data class Image ( val position: Int? = 0, val coor: IntArray = intArrayOf(0, 0) ) { override fun equals(o
val originalImage = imagesArray.find {it.position == 2}
我的图像类:
data class Image (
val position: Int? = 0,
val coor: IntArray = intArrayOf(0, 0)
) {
override fun equals(other: Any?): Boolean {
return super.equals(other)
}
override fun hashCode(): Int {
return super.hashCode()
}
}
我试图在数组中找到一个位置等于2的图像对象,并返回其索引。哦,你说得对,非常感谢