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.mzph.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亲子共读灯首发评测

论一束光的重要性你该听听一个高度近视孩子的自述,论童年陪伴的重要性你该听听一个留守儿童的自述,改善孩子童年的全球第一盏亲子共读台灯,贴合孩子与家长的心灵,量身打造每一种情境的光线去感受孩子成长学习过程 一个高度近视眼孩…