博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
array.slice_Ruby中带有示例的Array.slice()方法
阅读量:2531 次
发布时间:2019-05-11

本文共 4573 字,大约阅读时间需要 15 分钟。

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 declarationtable = [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)}"else	puts "Array index out of bound"endputs "Array instance after slicing: #{table}"

Output

输出量

Array slice implementationEnter the index you want to slice 4The element which is sliced is 10Array 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 declarationtable = [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)}"else	puts "Array index out of bound"endputs "Array instance after slicing: #{table}"

Output

输出量

Array slice implementationEnter the start index you want to slice 3Enter the length 5The 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中带来任何变化。

翻译自:

array.slice

转载地址:http://wttzd.baihongyu.com/

你可能感兴趣的文章
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>