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()方法 。 你們都必須認(rèn)為方法必須執(zhí)行與Array實(shí)例中的元素或?qū)ο笄衅嘘P(guān)的操作。 它并不像看起來(lái)那么簡(jiǎn)單。 好吧,我們將在其余內(nèi)容中解決這個(gè)問(wèn)題。 我們將嘗試借助語(yǔ)法并演示程序代碼來(lái)理解它。
Method description:
方法說(shuō)明:
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.
該方法是一個(gè)公共實(shí)例方法,為Ruby庫(kù)中的Array類定義。 此方法在元素引用上起作用,并在與方法調(diào)用一起傳遞的索引處返回元素。 如果您通過(guò)方法傳遞兩個(gè)參數(shù),并且這些參數(shù)分別是開(kāi)始和長(zhǎng)度,則該方法將返回一個(gè)子數(shù)組,該子數(shù)組將包含從開(kāi)始索引到長(zhǎng)度索引的元素。 在方法調(diào)用時(shí)將范圍作為參數(shù)傳遞的情況下,此方法將返回子數(shù)組。 此方法是非破壞性方法的示例之一,該方法不會(huì)對(duì)self Array中的對(duì)象的實(shí)際排列帶來(lái)任何改變。
Syntax:
句法:
array_instance.slice(index) -> object or nilorarray_instance.slice(start,length)-> new_array or nilorarray_instance.slice(range)-> new_array or nilArgument(s) required:
所需參數(shù):
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.
在方法調(diào)用時(shí),可以在此方法內(nèi)提供單個(gè)索引或范圍,起始和長(zhǎng)度作為參數(shù)。 您將根據(jù)在方法內(nèi)部傳遞的參數(shù)獲得輸出。
Example 1:
范例1:
=beginRuby 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_iif(table.slice(ind))puts "The element which is sliced is #{table.slice(ind)}" elseputs "Array index out of bound" endputs "Array instance after slicing: #{table}"Output
輸出量
Array slice implementation Enter the index you want to slice4 The element which is sliced is 10 Array instance after slicing: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]Explanation:
說(shuō)明:
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實(shí)例中切片元素 。 我們將在要求用戶輸入輸入值的索引的幫助下對(duì)其進(jìn)行切片。 已從Array實(shí)例中切片了第四個(gè)索引對(duì)象。 由于該方法是非破壞性方法的示例之一,因此該方法未在self Array中帶來(lái)任何變化。
Example 2:
范例2:
=beginRuby 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_iputs "Enter the length" len = gets.chomp.to_iif(table.slice(ind,len))puts "The sub array which is sliced is #{table.slice(ind,len)}" elseputs "Array index out of bound" endputs "Array instance after slicing: #{table}"Output
輸出量
Array slice implementation Enter the start index you want to slice3 Enter the length5 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:
說(shuō)明:
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實(shí)例的元素創(chuàng)建子數(shù)組的 。 我們?cè)趦蓚€(gè)參數(shù)的幫助下對(duì)其進(jìn)行切片,即我們要求用戶輸入值的起始索引和長(zhǎng)度。 在輸出中,你可以看到,Array實(shí)例已經(jīng)從第三索引,并導(dǎo)致其中包含五個(gè)對(duì)象的陣列的形成的7 個(gè)索引切片。 由于該方法是非破壞性方法的示例之一,因此該方法未在self Array中帶來(lái)任何變化。
翻譯自: https://www.includehelp.com/ruby/array-slice-method-with-example.aspx
array.slice
總結(jié)
以上是生活随笔為你收集整理的array.slice_Ruby中带有示例的Array.slice()方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: stack示例_C.示例中的Stack.
- 下一篇: Java短类的compareTo()方法