Home > database >  Difficulty Comparing Two Arrays In Kotlin
Difficulty Comparing Two Arrays In Kotlin

Time:01-09

Hi I can't seem to compare these two arrays on Kotlin.

var temp = ingredientsList.toArray()
var temp2 = chickenTikkaMasala.get(0).ingredients
if(ingredientsList.toArray().contentEquals(chickenTikkaMasala.get(0).ingredients)) {
   Toast.makeText(this, "Chicken Tikka Worked", Toast.LENGTH_SHORT).show()
}

Temp and Temp2 debug screenshot

Screenshot of Code

CodePudding user response:

The contentEquals function will only return true if the arrays contain the same elements in the same order. Your arrays doesn't have the same order. From the docs:

/**
 * Returns `true` if the two specified arrays are *structurally* equal to one another,
 * i.e. contain the same number of the same elements in the same order.
 *

CodePudding user response:

If you are only interested whether an item exists in the collection, then you really deal with sets here, not lists or arrays. So you need to compare them as sets:

ingredientsList.toSet() == chickenTikkaMasala.get(0).ingredients.toSet()
  •  Tags:  
  • Related