PHP - Ds Sequence::rotate() 函数



PHP 的 Ds\Sequence::rotate() 函数将序列旋转给定次数。

旋转以这样的方式执行:序列的元素向左移动指定的位数。如果旋转次数为负数,则元素改为向右移动。

如果旋转次数为正数,则旋转等效于连续调用“$sequence->push($sequence->shift())”,或者如果旋转次数为负数,则等效于“$sequence->unshift($sequence->pop())”。

语法

以下是 PHP Ds\Sequence::rotate() 函数的语法:

abstract public Ds\Sequence::rotate(int $rotations): void

参数

以下是此函数的参数:

  • rotations - 需要旋转序列的次数。

返回值

此函数不返回值。

示例 1

以下程序演示了 PHP Ds\Sequence::rotate() 函数的用法:

<?php  
   $seq = new \Ds\Vector([1, 2, 3, 4, 5]);  
   echo("The original sequence: \n");  
   print_r($seq);
   $rotate = 1;
   echo "The number of rotation: ".$rotate;
   #using the rotate() function
   $seq->rotate($rotate);
   echo("\nThe sequence after rotating by ".$rotate." places: \n");  
   print_r($seq);
?>

输出

执行上述程序后,它会生成以下输出:

The original sequence:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The number of rotation: 1
The sequence after rotating by 1 places:
Ds\Vector Object
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 1
)

示例 2

以下是 PHP Ds\Sequence::rotate() 函数的另一个示例。我们使用此函数将此序列 (['a', 'e', 'i', 'o', 'u']) 旋转给定的旋转次数 3

<?php  
   $seq = new \Ds\Vector(['a', 'e', 'i', 'o', 'u']);  
   echo("The original sequence: \n");  
   print_r($seq);
   $rotate = 3;
   echo "The number of rotation: ".$rotate;
   #using the rotate() function
   $seq->rotate($rotate);  
   echo("\nThe sequence after rotating by ".$rotate." places: \n");  
   print_r($seq);
?>

输出

上述程序产生以下输出:

The original sequence:
Ds\Vector Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The number of rotation: 3
The sequence after rotating by 3 places:
Ds\Vector Object
(
    [0] => o
    [1] => u
    [2] => a
    [3] => e
    [4] => i
)

示例 3

如果给定的 rotations 参数值为 负数,则 rotate() 函数将序列 逆时针旋转,如下所示:

<?php  
   $seq = new \Ds\Vector(["Tutorials", "Point", "India"]);  
   echo("The original sequence: \n");  
   print_r($seq);
   $rotate = -1;
   echo "The number of rotation: ".$rotate;
   #using the rotate() function
   $seq->rotate($rotate);  
   echo("\nThe sequence after rotating by ".$rotate." places: \n");  
   print_r($seq);
?>

输出

执行上述程序后,将显示以下输出:

The original sequence:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The number of rotation: -1
The sequence after rotating by -1 places:
Ds\Vector Object
(
    [0] => India
    [1] => Tutorials
    [2] => Point
)

示例 4

如果我们将旋转值传递为 ,则此函数将 不会旋转序列,并且原始序列将保持不变:

<?php  
   $seq = new \Ds\Vector([10, 20, 30]);  
   echo("The original sequence: \n");  
   print_r($seq);
   $rotate = 0;
   echo "The number of rotation: ".$rotate;
   #using the rotate() function
   $seq->rotate($rotate);  
   echo("\nThe sequence after rotating by ".$rotate." places: \n");  
   print_r($seq);
?>

输出

以下是上述程序的输出:

The original sequence:
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
)
The number of rotation: 0
The sequence after rotating by 0 places:
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
)
php_function_reference.htm
广告