To check if two arrays are equal in Kotlin, you can use the contentEquals
method of the Arrays class for arrays with the same elements, and the Arrays.equals
method for arrays with different elements.
In this example,
arr1
and arr2
with the same elements.contentEquals
method of the Arrays class to check if the arrays are equal and store the result in the variable equal1
.contentEquals
method returns true
if the arrays have the same elements in the same order, otherwise false
.equal1
to the console.import java.util.Arrays;
fun main() {
val arr1 = intArrayOf(1, 2, 3)
val arr2 = intArrayOf(1, 2, 3)
val equal1 = arr1.contentEquals(arr2)
println("Arrays are equal: $equal1")
}
Arrays are equal: true
In this example,
arr3
and arr4
with different elements.Arrays.equals
method to check if the arrays are equal and store the result in the variable equal2
.Arrays.equals
method compares the arrays for equality irrespective of the order of elements.equal2
to the console.import java.util.Arrays;
fun main() {
val arr3 = intArrayOf(1, 2, 3)
val arr4 = intArrayOf(7, 2, 8)
val equal2 = Arrays.equals(arr3, arr4)
println("Arrays are equal: $equal2")
}
Arrays are equal: false
In this tutorial, we learned How to check if Two Arrays are Equal in Kotlin language with well detailed examples.