函式名:Ds\Sequence::rotate()
適用版本:PHP 7.0.0 及以上版本
用法:該方法用於將序列中的元素按照指定的偏移量進行迴圈移動。偏移量可以是正數或負數。
方法簽名:
public function rotate(int $rotations): void
引數:
$rotations
:一個整數,表示要進行的迴圈移動的偏移量。正數表示向右移動,負數表示向左移動。
示例:假設我們有一個序列[1, 2, 3, 4, 5],我們將透過rotate()方法將其向右移動2個位置。
$sequence = new Ds\Vector([1, 2, 3, 4, 5]);
$sequence->rotate(2);
print_r($sequence);
輸出:
Ds\Vector Object
(
[0] => 4
[1] => 5
[2] => 1
[3] => 2
[4] => 3
)
在上面的示例中,序列[1, 2, 3, 4, 5]經過向右移動2個位置後,變為[4, 5, 1, 2, 3]。