日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

rotate array_Array.rotate! Ruby中的示例方法

發(fā)布時(shí)間:2025/3/11 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rotate array_Array.rotate! Ruby中的示例方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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! 方法 。 你們都必須認(rèn)為該方法必須執(zhí)行與旋轉(zhuǎn)某些元素有關(guān)的操作。 它并不像看起來那么簡單。 好吧,我們將在其余內(nèi)容中解決這個(gè)問題。 我們將嘗試借助語法并演示程序代碼來理解它。

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.

該方法是一個(gè)公共實(shí)例方法,為Ruby庫中的Array類定義。 此方法以旋轉(zhuǎn)Array實(shí)例內(nèi)部存在的內(nèi)容或?qū)ο蟮姆绞焦ぷ鳌?它以以下方式旋轉(zhuǎn)Array元素:將第二個(gè)元素視為第一個(gè)元素,將最后一個(gè)對象視為Array類的對象的第一個(gè)元素。 如果在方法內(nèi)部傳遞任何負(fù)整數(shù),則旋轉(zhuǎn)方向相反。 Array.rotate! 是一種破壞性方法 ,其中此方法創(chuàng)建的更改將對Self Array實(shí)例的實(shí)際順序產(chǎn)生影響,并且這些更改是永久的。

Syntax:

句法:

array_instance.rotate! -> new_arrayorarray_instance.rotate!(count) -> new_array

Argument(s) required:

所需參數(shù):

This method does take one argument and that argument decides from which index the rotation is going to be held.

此方法確實(shí)采用一個(gè)參數(shù),并且該參數(shù)決定將從哪個(gè)索引中保留旋轉(zhuǎn)。

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 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旋轉(zhuǎn)Array類實(shí)例的內(nèi)容! 方法 。 您可以觀察到旋轉(zhuǎn)內(nèi)容之后,第一個(gè)元素是“ Java” ,最后一個(gè)元素是“ C ++” 。 由于此方法是一種破壞性方法,因此它對Array實(shí)例中元素的實(shí)際排列產(chǎn)生了影響。

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 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方法 ,我們正在旋轉(zhuǎn)Array類實(shí)例的內(nèi)容 。 由于我們在方法內(nèi)部傳遞了一個(gè)負(fù)整數(shù),因此旋轉(zhuǎn)從Array實(shí)例的后端開始,而第二個(gè)元素從最后一個(gè)開始。 您可以觀察到,在旋轉(zhuǎn)內(nèi)容之后,第一個(gè)元素為18,因?yàn)榇朔椒ň哂衅茐男?#xff0c;并且會(huì)在Array實(shí)例中更改內(nèi)容的實(shí)際排列。

翻譯自: https://www.includehelp.com/ruby/array-rotate-inverse-method-with-example.aspx

rotate array

總結(jié)

以上是生活随笔為你收集整理的rotate array_Array.rotate! Ruby中的示例方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。