Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 489 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rotate kotlin array

#1
Assume that I have an array like `1 2 3 4 5`, I want to rotate it to the left by **n** and get a new one.

For example the 2 rotation of the above array will result in `3 4 5 1 2`. I didn't found any extension function to do that.
Reply

#2
You can use built-in java `Collections.rotate` method, but you need to convert your array to list firstly:

val arr = intArrayOf(1, 2, 3, 4, 5)
val list = arr.toList()
Collections.rotate(list, -2)
println(list.toIntArray().joinToString())

Outputs

3, 4, 5, 1, 2
Reply

#3
Another extension function, by slicing the array in 2 parts `left` and `right` and reassembling it to `right + left`: <br/>

fun <T> Array<T>.leftShift(d: Int) {
val n = d % this.size // just in case
if (n == 0) return // no need to shift

val left = this.copyOfRange(0, n)
val right = this.copyOfRange(n, this.size)
System.arraycopy(right, 0, this, 0, right.size)
System.arraycopy(left, 0, this, right.size, left.size)
}

so this: <br/>

val a = arrayOf(1, 2, 3, 4, 5, 6, 7)
a.leftShift(2)
a.forEach { print(" " + it) }

will print <br/>

3 4 5 6 7 1 2
Reply

#4
For the record, you can use the regular [`Array` constructor](

[To see links please register here]

) to build a new array:

inline fun <reified T> Array<T>.rotate(n: Int) = Array(size) { this[(it + n) % size] }

The element at index `it` in the source array is copied in the destination array at the new index `(it + n) % size` to perform the rotation.

It is a bit slower than copying the array by chunks.
Reply

#5
Simple solution:
```kotlin
fun <T> Array<T>.rotateLeft(n: Int) = drop(n) + take(n)
fun <T> Array<T>.rotateRight(n: Int) = takeLast(n) + dropLast(n)
```
The limitation is that `n` must be less than or equal to the length of the array.

Alternatively, you can use `Collections.rotate(...)` as follows.

```kotlin
import java.util.Collections

fun <T> Array<T>.rotate(distance: Int) =
toList().also { // toList() is a deep copy to avoid changing the original array.
Collections.rotate(it, distance)
}

fun main() {
val xs = arrayOf(1, 2, 3, 4, 5)
val ys = xs.rotate(-2)
xs.forEach { print("$it ") } // 1 2 3 4 5
println(ys) // [3, 4, 5, 1, 2]
}
```
Reply

#6
I interpret *"get a new one"* to mean that the extension function should return a new array instance, like so (boundary checks omitted, `sliceArray` is an [stdlib function][1]) :


fun <T> Array<T>.rotate(n: Int) =
let { sliceArray(n until size) + sliceArray(0 until n) }

**Example**

```kotlin
arrayOf(1, 2, 3, 4, 5).rotate(1)
.also { println(it.joinToString()) } // 2, 3, 4, 5, 1
```


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through