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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

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

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實例中的元素或?qū)ο笄衅嘘P(guān)的操作。 它并不像看起來那么簡單。 好吧,我們將在其余內(nèi)容中解決這個問題。 我們將嘗試借助語法并演示程序代碼來理解它。

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

Syntax:

句法:

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

Argument(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)用時,可以在此方法內(nèi)提供單個索引或范圍,起始和長度作為參數(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:

說明:

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實例中切片元素 。 我們將在要求用戶輸入輸入值的索引的幫助下對其進(jìn)行切片。 已從Array實例中切片了第四個索引對象。 由于該方法是非破壞性方法的示例之一,因此該方法未在self Array中帶來任何變化。

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:

說明:

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實例的元素創(chuàng)建子數(shù)組的 。 我們在兩個參數(shù)的幫助下對其進(jìn)行切片,即我們要求用戶輸入值的起始索引和長度。 在輸出中,你可以看到,Array實例已經(jīng)從第三索引,并導(dǎo)致其中包含五個對象的陣列的形成的7 索引切片。 由于該方法是非破壞性方法的示例之一,因此該方法未在self Array中帶來任何變化。

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

array.slice

總結(jié)

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

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

主站蜘蛛池模板: 国产精品久久久久久久蜜臀 | 日韩色av | 一二三不卡视频 | 日韩国产一级 | 天天想你在线观看完整版电影免费 | 超碰牛牛| 天天插天天插 | 成都4电影免费高清 | 精品久久久久一区 | 91成品视频 | 日韩激情片 | 国产富婆一级全黄大片 | 污视频网站免费看 | 精品黑人| 人妻中文字幕一区 | 亚洲在线不卡 | 国产一级一片 | 欧洲av一区二区 | 久久影视精品 | 国产日韩一区二区三免费高清 | 久久免费播放视频 | 乌克兰毛片 | av毛片在线播放 | 色妞av | 可以在线观看的av网站 | 亚洲一区网 | 91久久亚洲 | 中文字幕亚洲乱码熟女1区2区 | 久久夜夜操妹子 | 环太平洋3:泰坦崛起 | 黑人玩弄人妻一区二区三区免费看 | 久久色视频 | 日韩色网 | 日韩欧美成人网 | 欧美一区二区三区激情视频 | 亚洲婷婷丁香 | 女人性做爰100部免费 | 狠狠爱亚洲 | 加勒比在线一区 | 91香焦视频 | 人人射人人射 | 久久精品国产99国产 | a天堂在线观看视频 | 国产精品一区二区在线播放 | av青青草原 | 成人福利视频 | 中国一区二区视频 | www.九九九| 国产日本一区二区三区 | 吻胸摸激情床激烈视频大胸 | 成人午夜视频在线免费观看 | 日产精品久久久久 | 日本黄色一级 | 人妻精品一区二区在线 | 亚洲图片自拍偷拍区 | 亚洲精品v| 91精品国产综合久久久密臀九色 | 亚洲精品自拍 | 青草青草久热 | 饥渴少妇色诱水电工 | 精品妇女一区二区三区 | 搡8o老女人老妇人老熟 | 欧美激情综合网 | 在线观看免费黄视频 | 国产理论影院 | 黄色视屏网站 | 优优色影院 | 国产欧美日韩在线播放 | 色婷婷av一区二区三区软件 | 成年人在线视频观看 | 日本肉体xxxx裸体xxx免费 | 欧美裸体网站 | 亚洲经典在线 | 日韩成人av影院 | 亚洲人成777 | 日韩av免费在线播放 | 尤物videos另类xxxx | 欧美性猛交xxxx久久久 | 精国产人伦一区二区三区 | 天天色综合色 | 国产福利视频网站 | 国产一区二区精品久久 | 亚洲激情三区 | 日韩高清专区 | 日韩成人影视 | 日韩精品成人 | 免费黄色一级大片 | 色吧av| aa一级黄色片 | 欧美精品一区二区三区久久 | 国产这里只有精品 | 性xxxx另类xxⅹ | 天天操操操操 | 亚洲精品乱码久久久久99 | 久久视频一区二区 | 簧片av| 精品人妻无码一区 | 希岛婚前侵犯中文字幕在线 | 五十路黄色片 |