array.slice_Ruby中帶有示例的Array.slice()方法

array.slice

Array.slice()方法 (Array.slice() Method)

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

Method description:

方法說明:

This method is a public instance method and defined for the Array class in Ruby's library. This method works on element reference and returns the element at the index which is passed with the method invocation. If you are passing two parameters with the method and those parameters are the start and the length then the method will return a subarray which will contain the elements from the start index and till the length index. This method will return subarray in the case when the range is passed as the parameter at the time of method invocation. This method is one of the examples of the non-destructive method where the method does not bring any change in the actual arrangement of objects in the self Array.

該方法是一個公共實例方法,為Ruby庫中的Array類定義。 此方法在元素引用上起作用,并在與方法調用一起傳遞的索引處返回元素。 如果您通過方法傳遞兩個參數,并且這些參數分別是開始和長度,則該方法將返回一個子數組,該子數組將包含從開始索引到長度索引的元素。 在方法調用時將范圍作為參數傳遞的情況下,此方法將返回子數組。 此方法是非破壞性方法的示例之一,該方法不會對self Array中的對象的實際排列帶來任何改變。

Syntax:

句法:

    array_instance.slice(index) -> object or nil
or
array_instance.slice(start,length)-> new_array or nil
or
array_instance.slice(range)-> new_array or nil

Argument(s) required:

所需參數:

You can provide a single index or range or start and length as the argument inside this method at the time of method call. You will get the output on the basis of the argument you pass inside the method.

在方法調用時,可以在此方法內提供單個索引或范圍,起始和長度作為參數。 您將根據在方法內部傳遞的參數獲得輸出。

Example 1:

范例1:

=begin
Ruby program to demonstrate slice method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array slice implementation"
puts "Enter the index you want to slice"
ind = gets.chomp.to_i
if(table.slice(ind))
puts "The element which is sliced is #{table.slice(ind)}"
else
puts "Array index out of bound"
end
puts "Array instance after slicing: #{table}"

Output

輸出量

Array slice implementation
Enter the index you want to slice
4
The element which is sliced is 10
Array instance after slicing: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Explanation:

說明:

In the above code, you can observe that we are slicing the element from the Array instance with the help of Array.slice() method. We are slicing it with the help of an index for which we have asked the user to an input value. The 4th index object has been sliced from the Array instance. The method is not bringing changes in the self Array due to the fact that this method is one of the examples of non-destructive methods.

在上面的代碼中,您可以觀察到我們正在借助Array.slice()方法從Array實例切片元素 。 我們將在要求用戶輸入輸入值的索引的幫助下對其進行切片。 已從Array實例中切片了第四個索引對象。 由于該方法是非破壞性方法的示例之一,因此該方法未在self Array中帶來任何變化。

Example 2:

范例2:

=begin
Ruby program to demonstrate slice method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array slice implementation"
puts "Enter the start index you want to slice"
ind = gets.chomp.to_i
puts "Enter the length"
len = gets.chomp.to_i
if(table.slice(ind,len))
puts "The sub array which is sliced is #{table.slice(ind,len)}"
else
puts "Array index out of bound"
end
puts "Array instance after slicing: #{table}"

Output

輸出量

Array slice implementation
Enter the start index you want to slice
3
Enter the length
5
The sub array which is sliced is [8, 10, 12, 14, 16]
Array instance after slicing: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Explanation:

說明:

In the above code, you can observe that we are creating a subarray from the elements of the Array instance with the help of Array.slice() method. We are slicing it with the help of two parameters which namely start index and length for which we have asked the user to input values. In the output, you can see that the Array instance has been sliced from the 3rd index and to the 7th index resulting in the formation of an Array which contains five objects. The method is not bringing changes in the self Array due to the fact that this method is one of the examples of the non-destructive methods.

在上面的代碼中,您可以觀察到我們是在Array.slice()方法的幫助下從Array實例的元素創建子數組的 。 我們在兩個參數的幫助下對其進行切片,即我們要求用戶輸入值的起始索引和長度。 在輸出中,你可以看到,Array實例已經從第三索引,并導致其中包含五個對象的陣列的形成的7 索引切片。 由于該方法是非破壞性方法的示例之一,因此該方法未在self Array中帶來任何變化。

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

array.slice

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541026.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541026.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541026.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

阿特斯攜手EDF啟動建設巴西191.5MW光伏項目

2016年10月11日,阿特斯太陽能(安大略省,圭爾夫)和EDF Energies Nouvelles(法國,巴黎)共同宣布,將阿特斯巴西Pirapora I太陽能項目80%的股權出售給EDF的巴西本地子公司EDF…

apachejmeter_java源碼_自定義編寫jmeter的Java測試代碼

我們在做性能測試時,有時需要自己編寫測試腳本,很多測試工具都支持自定義編寫測試腳本,比如LoadRunner就有很多自定義腳本的協議,比如"C Vuser","JavaVuser"等協議.同樣,Jmeter也支持自定義編寫的測試代碼,不過與LoadRunner不同的是,Jmeter沒有自帶編譯器,…

julia fit 函數_帶有Julia中示例的flipsign()函數

julia fit 函數Julia| flipsign()函數 (Julia | flipsign() function) flipsign() function is a library function in Julia programming language, it accepts two values as parameters and returns a value with the magnitude of first value and sign of the first value…

優化Android應用內存的若干方法

https://my.oschina.net/chaselinfo/blog/198172摘要: 在app開發的各個階段中要考慮RAM的限制問題, 包括在設計階段(正式開發之前). 使用下面的不同的方法可以達到很好的效果. 當您在設計和開發Android應用時用下面的方法可以使內存運用最高效.使用保守的Service 如果你的應用需…

一? ilkkn.n_IL&FS的完整形式是什么?

一? il&kkn.nIL&FS:基礎設施租賃和金融服務 (IL& FS: Infrastructure Leasing & Financial Services) IL&FS is an abbreviation of Infrastructure Leasing & Financial Services. It is the largest infrastructure development …

java notify喚醒原理_Java wait和notify虛假喚醒原理

自己在此記錄一下,方便日后復習。虛假喚醒的概念jdk官方文檔解釋:所以說在wait和notify一塊使用時,如果使用if作為條件時,會有虛假喚醒的情況發生,所以必須使用while作為循環條件。下面來舉例實驗:首先&…

C#里面的三種定時計時器:Timer

在.NET中有三種計時器:1、System.Windows.Forms命名空間下的Timer控件,它直接繼承自Componet。Timer控件只有綁定了Tick事件和設置EnabledTrue后才會自動計時,停止計時可以用Stop()方法控制,通過Stop()停止之后,如果想…

wireshark rto_RTO的完整形式是什么?

wireshark rtoRTO:地區運輸辦公室/公路運輸辦公室 (RTO: Regional Transport Office/ Road Transport Office) RTO is an abbreviation of the Regional Transport Office. It is an Indian Government departmental organization that is responsible for upholdin…

java8 json轉xml_2019-08-17java對象與json,xml互轉

依賴的jar包,jackson-all-1.7.6.jar,xstream-1.4.4.jar下載地址:鏈接:https://pan.baidu.com/s/1LflD135qlQiIPGXw5XwDmw提取碼:6v29復制這段內容后打開百度網盤手機App,操作更方便哦package json_xml;import com.thoughtworks.xs…

10.8-全棧Java筆記:序列化/反序列化的步驟和實例

本節我們詳細講解10.3節中提到的序列化和反序列化操作。序列化和反序列化是什么當兩個進程遠程通信時,彼此可以發送各種類型的數據。 無論是何種類型的數據,都會以二進制序列的形式在網絡上傳送。比如,我們可以通過http協議發送字符串信息&am…

有效的網絡推廣超級實用方法

我叫龍雨,先后在百度搜狗工作過3年,后來一直負責一家公司的的網絡營銷!不知道大家有沒有聽過111>3這樣一個概念,簡單來說一下這概念!第一呢就是自己的資源,把自己的資源維護好開發好;第二就是網絡營銷,網絡營銷利用…

什么為java運行時的環境_什么是JRE?Java運行時環境簡介(一)

Java開發工具包(JDK),Java虛擬機(JVM)和Java運行時環境(JRE)共同構成了用于開發和運行Java應用程序的Java平臺組件的強大功能.實際上,運行時環境是一種旨在運行其他軟件的軟件.作為Java的運行時環境,JRE包含Java類庫,Java類加載器和Java虛擬機.在這個系統中:的類加載器是負責正…

c語言atoll函數怎么用_C ++中帶有示例的atoll()函數

c語言atoll函數怎么用C Atoll()函數 (C atoll() function) atoll() function is a library function of cstdlib header. It is used to convert the given string value to the integer value. It accepts a string containing an integer (integral) number and returns its…

看清美國“黑客帝國”的真面目

“維基揭秘”網站近日發布了近9000份據稱屬于美國中央情報局的機密文件,顯示中情局擁有強大的黑客攻擊能力,秘密侵入了手機、電腦、智能電視等眾多智能設備。繼美國國家安全局承包商前雇員斯諾登曝光國安局“棱鏡”等監控計劃之后,此次曝光再…

python 示例_帶有示例的Python File close()方法

python 示例文件close()方法 (File close() Method) close() method is an inbuilt method in Python, it is used to flush and close the IO object (file). If we close a closed file it has no effect. close()方法是Python中的內置方法,用于刷新和關閉IO對象(…

linux上mysql分區磁盤位置_Linux下Oracle軟件、數據文件等所在的磁盤分區空間不足的解決思路...

虛擬機中的ORACLE運行的久了,歸檔、數據文件不斷增長,原來安裝ORACLE的分區空間不足。此時可以重新向虛擬機增加一塊硬盤,將OR虛擬機中的Oracle運行的久了,歸檔、數據文件不斷增長,原來安裝ORACLE的分區空間不足。此時…

FloatingActionMenu 向上彈出菜單

本人在github上找到了一個FloatingActionsMenu,精簡了其效果(原效果有上下左右四個方向)僅僅保留向上的效果,并做了一定的優化。github上的源代碼:地址 ,精簡后的源代碼地址:源代碼地址。 轉載于:https://www.cnblogs.…

java uuid靜態方法_Java UUID的variant()方法和示例

java uuid靜態方法UUID類variant()方法 (UUID Class variant() method) variant() method is available in java.util package. variant()方法在java.util包中可用。 variant() method is used to get the variant number linked with this UUID. variant()方法用于獲取與此UUI…

java程序服務不能啟動不了_JavaService.exe注冊的windows服務無法啟動問題

最近開發了個java程序,打成 jar 包想要在windows后臺運行,于是使用JavaService.exe進行windows服務注冊,服務注冊很成功,但是在啟動時顯示“服務無法啟動”,為此耗費了大量的時間與精力,終于發現問題所在&a…

給孩子一束安全的光 明基WiT MindDuo親子共讀燈首發評測

論一束光的重要性你該聽聽一個高度近視孩子的自述,論童年陪伴的重要性你該聽聽一個留守兒童的自述,改善孩子童年的全球第一盞親子共讀臺燈,貼合孩子與家長的心靈,量身打造每一種情境的光線去感受孩子成長學習過程 一個高度近視眼孩…