rotate array_Array.rotate! Ruby中的示例方法
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_arrayorarray_instance.rotate!(count) -> new_arrayArgument(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:
=beginRuby 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 langOutput
輸出量
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:
=beginRuby 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 tableOutput
輸出量
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
總結
以上是生活随笔為你收集整理的rotate array_Array.rotate! Ruby中的示例方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: strtolower_PHP strto
- 下一篇: 2G的完整形式是什么?