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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

ruby array_Ruby中带有示例的Array.fill()方法(1)

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

ruby array

Array.fill()方法 (Array.fill() Method)

In this article, we will study about Array.fill() method. You all must be thinking the method must be doing something related to populate the Array instance. Well, we will figure this out in the rest of our content.

在本文中,我們將研究Array.fill()方法 。 你們都必須認(rèn)為該方法必須做一些與填充Array實(shí)例有關(guān)的事情。 好吧,我們將在其余內(nèi)容中解決這個(gè)問題。

Method description:

方法說明:

This method is one of the examples of the Public instance method which is specially defined in the Ruby library for Array class. This method is used to populate the Array instances. You can fill multiple objects in the object of the Array class with the help of this method. This method is one of the examples of Destructive methods. This method has many forms and we will be studying them in the rest of the content. There are two of its types are present in this article and are demonstrated with the help of syntaxes and program codes.

此方法是Ruby類庫中為Array類專門定義的Public實(shí)例方法的示例之一。 此方法用于填充Array實(shí)例。 您可以借助此方法在Array類的對象中填充多個(gè)對象。 此方法是破壞性方法的示例之一。 這種方法有多種形式,我們將在其余內(nèi)容中對其進(jìn)行研究。 本文介紹了它的兩種類型,并在語法和程序代碼的幫助下進(jìn)行了演示。

Type 1: fill(obj) -> arr

類型1:填充(obj)-> arr

The Array instance will be populated with the object which is passed with the method.

Array實(shí)例將使用該方法傳遞的對象填充。

Syntax:

句法:

array_instance.fill(object)

Example 1:

范例1:

=beginRuby program to demonstrate fill method =end# array declaration array1 = ["Kumar","Ramesh","Apple","Pappu","Sana","Yogita","Satyam","Harish"]puts "Array fill implementation." puts "Enter the element you want to insert" ele = gets.chomp array1.fill(ele)puts "Array elements are:" puts array1

Output

輸出量

Array fill implementation. Enter the element you want to insertvasu Array elements are: vasu vasu vasu vasu vasu vasu vasu vasu

Explanation:

說明:

You can observe in the above example that when the object is passed with the method then it has overwritten all the elements present in the Array instances which we stored at the time of declaration of the Array instance.

您可以在上面的示例中觀察到,當(dāng)對象與方法一起傳遞時(shí),該對象將覆蓋Array實(shí)例中存在的所有元素,這些元素在聲明Array實(shí)例時(shí)存儲。

Type 2: fill(obj, start [, length])

類型2:fill(obj,start [,length])

This method will not populate the Array instance with the same object. In this method, you will have to pass the object along with the index from where you want to insert the element and up to where you want to populate the Array instance with the same object.

此方法將不會使用相同的對象填充Array實(shí)例。 在此方法中,您將必須將對象與索引一起從您要插入元素的位置傳遞到您要使用相同對象填充Array實(shí)例的位置。

Syntax:

句法:

array_instance.fill(obj,start[,length])

Example 2:

范例2:

=beginRuby program to demonstrate fill method =end# array declaration array1 = ["Kumar","Ramesh","Apple","Pappu","Sana","Yogita","Satyam","Harish"]puts "Array fill implementation."puts "Enter the element you want to insert:" ele = gets.chompputs "From where you want to start populating:" st = gets.chomp.to_iputs "Up to where you want to start populating:" pp = gets.chomp.to_iarray1.fill(ele,st,pp)puts "Array elements are:" puts array1

Output

輸出量

Array fill implementation. Enter the element you want to insert:Amisha From where you want to start populating:2 Up to where you want to start populating:4 Array elements are: Kumar Ramesh Amisha Amisha Amisha Amisha Satyam Harish

Explanation:

說明:

In the above code, you can observe that we are asking the user for the object, form, and length. The user has entered 3 as the starting index and 3 is the number of repetitions of the object. So, you can observe that the object has been inserted at the 3rd index and has been repeated for three times by overwriting the already present elements of the object of Array class.

在上面的代碼中,您可以觀察到我們正在向用戶詢問對象,形式和長度。 用戶已輸入3作為起始索引,并且3是對象的重復(fù)次數(shù)。 因此,您可以觀察到該對象已插入第3個(gè)索引,并且通過覆蓋Array類的對象已存在的元素而重復(fù)了3次。

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

ruby array

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

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

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