rotate array
Array.rotate! 方法 (Array.rotate! Method)
In this article, we will study about Array.rotate! method. You all must be thinking the method must be doing something which is related to rotating certain elements. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.
在本文中,我们将研究Array.rotate! 方法 。 你们都必须认为该方法必须执行与旋转某些元素有关的操作。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。
Method description:
方法说明:
This method is a public instance method and defined for the Array class in Ruby's library. This method works in such a way that it rotates the content or objects present inside the Array instances. It rotates the Array elements in the way that the second element is considered to be first and the last object is considered to be the first element of the object of Array class. If you pass any negative integer inside the method then the rotation is done in the opposite direction. Array.rotate! is a destructive method where the changes created by this method would create an impact on the actual order of the Self Array instance and these changes are permanent.
该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法以旋转Array实例内部存在的内容或对象的方式工作。 它以以下方式旋转Array元素:将第二个元素视为第一个元素,将最后一个对象视为Array类的对象的第一个元素。 如果在方法内部传递任何负整数,则旋转方向相反。 Array.rotate! 是一种破坏性方法 ,其中此方法创建的更改将对Self Array实例的实际顺序产生影响,并且这些更改是永久的。
Syntax:
句法:
array_instance.rotate! -> new_array
or
array_instance.rotate!(count) -> new_array
Argument(s) required:
所需参数:
This method does take one argument and that argument decides from which index the rotation is going to be held.
此方法确实采用一个参数,并且该参数决定将从哪个索引中保留旋转。
Example 1:
范例1:
=begin
Ruby program to demonstrate rotate! method
=end
# array declaration
lang = ["C++","Java","Python","Html","Javascript","php","Ruby","Kotlin"]
puts "Array rotate! implementation."
print lang.rotate!
puts ""
puts "The first element of the Array is: #{lang[0]}"
puts "Array elements are:"
print lang
Output
输出量
Array rotate! implementation.
["Java", "Python", "Html", "Javascript", "php", "Ruby", "Kotlin", "C++"]
The first element of the Array is: Java
Array elements are:
["Java", "Python", "Html", "Javascript", "php", "Ruby", "Kotlin", "C++"]
Explanation:
说明:
In the above code, you can observe that we are rotating the contents of Array class instance with the help of Array.rotate! method. You can observe that after rotating the contents, the first element is "Java" and the last element is "C++". Due to the fact that this method is a destructive method, it is creating an impact on the actual arrangements of elements in the Array instance.
在上面的代码中,您可以观察到我们正在借助Array.rotate旋转Array类实例的内容! 方法 。 您可以观察到旋转内容之后,第一个元素是“ Java” ,最后一个元素是“ C ++” 。 由于此方法是一种破坏性方法,因此它对Array实例中元素的实际排列产生了影响。
Example 2:
范例2:
=begin
Ruby program to demonstrate rotate! method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array rotate! implementation"
print table.rotate!(-2)
puts ""
puts "The first element of the Array is: #{table.first}"
puts "Array elements are:"
print table
Output
输出量
Array rotate! implementation
[18, 20, 2, 4, 6, 8, 10, 12, 14, 16]
The first element of the Array is: 18
Array elements are:
[18, 20, 2, 4, 6, 8, 10, 12, 14, 16]
Explanation:
说明:
In the above code, you can observe that this method works on Integer Array as well and we are rotating the contents of Array class instance with the help of Array.rotate method. Since we are passing a negative integer inside the method, the rotation is started from the back end side of the Array instance and the 2nd element from the last. You can observe that after rotating the contents, the first element is 18 because this method is destructive and creates changes in the actual arrangements of contents in the Array instance.
在上面的代码中,您可以观察到该方法也适用于Integer Array,并且借助于Array.rotate方法 ,我们正在旋转Array类实例的内容 。 由于我们在方法内部传递了一个负整数,因此旋转从Array实例的后端开始,而第二个元素从最后一个开始。 您可以观察到,在旋转内容之后,第一个元素为18,因为此方法具有破坏性,并且会在Array实例中更改内容的实际排列。
翻译自: https://www.includehelp.com/ruby/array-rotate-inverse-method-with-example.aspx
rotate array