- What is the difference between:
reverse()
reversed()
reversedArray()
· Well when we think about like what was the purpose of this thing , to create same methods which will do the same this thi really makes a sense but these functions in kotlin not will do same thing actually each of those have their own specific job
reverse() - this method turns array/collections element into reverse like this:
[1,2,3,4,5] -> [5,4,3,2,1] but you cannot absorb it to another value like this:
var c = myArray.reverse() // c is now Unit not array which gives us nothing
And that's why we got a reversed() and reversedArray() methods which will helps us to get a reversed value from a List or Array and it will not effects for the original array like this:
var myArray: Array = arrayOf(1, 2, 3, 4, 5) // org array
var c = myArray.reversed() // List = [5,4,3,2,1]
var d = myArray.reversedArray() // Array = [5,4,3,2,1]
And those methods doesn't effects for the original array
reverse()
reversed()
reversedArray()
· Well when we think about like what was the purpose of this thing , to create same methods which will do the same this thi really makes a sense but these functions in kotlin not will do same thing actually each of those have their own specific job
reverse() - this method turns array/collections element into reverse like this:
[1,2,3,4,5] -> [5,4,3,2,1] but you cannot absorb it to another value like this:
var c = myArray.reverse() // c is now Unit not array which gives us nothing
And that's why we got a reversed() and reversedArray() methods which will helps us to get a reversed value from a List or Array and it will not effects for the original array like this:
var myArray: Array = arrayOf(1, 2, 3, 4, 5) // org array
var c = myArray.reversed() // List = [5,4,3,2,1]
var d = myArray.reversedArray() // Array = [5,4,3,2,1]
And those methods doesn't effects for the original array