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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

array.unshift_Ruby中带有示例的Array.unshift()方法

發布時間:2025/3/11 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 array.unshift_Ruby中带有示例的Array.unshift()方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

array.unshift

Array.unshift()方法 (Array.unshift() Method)

In this article, we will study about Array.unshift() Method. You all must be thinking the method must be doing something which is related to unshifting of objects in the Array instance. 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.unshift()方法 。 你們都必須認為該方法必須執行與Array實例中對象的移位相關的操作。 它并不像看起來那么簡單。 好吧,我們將在其余內容中解決這個問題。 我們將嘗試借助語法并演示程序代碼來理解它。

Method description:

方法說明:

This method is a public instance method and defined for the Array class in Ruby's library. This method works in a way that affixes the object which is provided with the method at the time of its invocation to the front of the self Array object. It simply means that the object will be shifted to the 0th index and the indices of rest objects are incremented by one. You can provide more than one argument to the method at the time of its invocation. This method is one of the examples of destructive methods where the changes created by the method are permanent. There is no non-destructive version of this method.

該方法是一個公共實例方法,為Ruby庫中的Array類定義。 此方法的工作方式是在調用方法時將隨方法提供的對象附加到self Array對象的前面。 這僅表示該對象將移至第0個索引,其余對象的索引將增加1。 調用方法時,可以為該方法提供多個參數。 此方法是破壞性方法的示例之一,該方法所創建的更改是永久性的。 沒有此方法的非破壞性版本。

Syntax:

句法:

array_instance.unshift(object) -> array

Argument(s) required:

所需參數:

This method can take n number of objects. You can even provide an Array instance as the argument.

此方法可以使用n個對象。 您甚至可以提供一個Array實例作為參數。

Example 1:

范例1:

=beginRuby program to demonstrate unshift method =end# array declaration table = [2,4,8,10,12,134,160,180,200,344]puts "Array unshift implementation"puts "Enter the number of objects you want to add:" num = gets.chomp.to_ifor i in 1..numputs "Enter the object:"ele = gets.chomptable.unshift(ele) endputs "The final Array instance: #{table}"

Output

輸出量

Array unshift implementation Enter the number of objects you want to add:3 Enter the object:Hrithik Enter the object:Amisha Enter the object:Satyam The final Array instance: ["Satyam", "Amisha", "Hrithik", 2, 4, 8, 10, 12, 134, 160, 180, 200, 344]

Explanation:

說明:

In the above code, you can observe that we are adding elements in the Array instance with the help of the Array.unshift() method. In the output, you can see that the objects which are given by the user are stored from 0th index. The previously present elements are moved upward.

在上面的代碼中,您可以觀察到,借助于Array.unshift()方法 ,我們正在Array實例中添加元素。 在輸出中,您可以看到用戶指定的對象是從 0 索引開始存儲的。 先前存在的元素向上移動。

Example 2:

范例2:

=beginRuby program to demonstrate unshift method =end# array declaration table = [2,4,8,10,12,134,160,180,200,344]puts "Array unshift implementation" Name = ["Ayush","Saksham","Nikhil"]table.unshift(Name)puts "The final Array instance: #{table}"

Output

輸出量

Array unshift implementation The final Array instance: [["Ayush", "Saksham", "Nikhil"], 2, 4, 8, 10, 12, 134, 160, 180, 200, 344]

Explanation:

說明:

In the above code, you can observe that we have prepended an Array instance in the self Array. Now, it has become a subarray of the self Array which is stored at the 0th index of the self Array.

在上面的代碼中,您可以觀察到我們已經在self Array中添加了Array實例。 現在,它已成為self Array的子??數組,存儲在self Array的第0 索引處。

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

array.unshift

總結

以上是生活随笔為你收集整理的array.unshift_Ruby中带有示例的Array.unshift()方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。