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

array.slice_Ruby中带有示例的Array.slice()方法相关推荐

  1. array.unshift_Ruby中带有示例的Array.unshift()方法

    array.unshift Array.unshift()方法 (Array.unshift() Method) In this article, we will study about Array. ...

  2. repeated_Ruby中带有示例的Array.repeated_combination()方法

    repeated Array.repeated_combination()方法 (Array.repeated_combination() Method) In this article, we wi ...

  3. ruby array_Ruby中带有示例的Array.shuffle方法

    ruby array Array.shuffle方法 (Array.shuffle Method) In this article, we will study about Array.shuffle ...

  4. ruby array_Ruby中带有示例的Array.fill()方法(1)

    ruby array Array.fill()方法 (Array.fill() Method) In this article, we will study about Array.fill() me ...

  5. obj[]与obj._Ruby中带有示例的Array.include?(obj)方法

    obj[]与obj. Ruby Array.include?(obj)方法 (Ruby Array.include?(obj) Method) In the previous articles, we ...

  6. ruby array_Ruby中带有示例的Array.index()方法

    ruby array Array.index()方法 (Array.index() Method) In this article, we will study about Array.index() ...

  7. ruby array_Ruby中带有示例的Array.sample()方法

    ruby array Array.sample()方法 (Array.sample() Method) In this article, we will study about Array.sampl ...

  8. ruby array_Ruby中带有示例的Array.fill()方法(3)

    ruby array Array.fill()方法 (Array.fill() Method) In this article, we will study about Array.fill() me ...

  9. ruby array_Ruby中带有示例的Array.select方法

    ruby array Array.select方法 (Array.select Method) In the last articles, we have seen how to iterate ov ...

最新文章

  1. 白月黑羽教python excel_发布程序
  2. JavaScript对象——原型与原型链
  3. Wine cannot find the ncurses library (libncurses.so.5)
  4. nodejs简单层级结构配置文件
  5. 使用libsvm中的svm_cross_validation函数进行交叉验证
  6. 2021年中国零售OMO研究报告
  7. 一个简单的PHP Web论坛
  8. End-to-end Recovery of Human Shape and Pose
  9. 新工作 Day15 周四
  10. 专题四:MATLAB绘图
  11. mysql数据库导入导出sql文件
  12. windows socket 网络编程
  13. 计算机英语听力速记...,2019计算机考研英语听力速记技巧才是王道
  14. 销售管理系统c语言 总结报告,C语言课程设计报告-药品销售管理系统.doc
  15. 【书影观后感 八】《周期》万事皆周期
  16. buzz fizz 翻译_【Oxford-2】The Fizz-Buzz
  17. eeepc linux 窗口管理器,EeePC安装Windows 7全教程 全机型适用
  18. android:scheme 常用类型,android scheme
  19. 梭子鱼网络:2018年网络安全威胁预测
  20. Android资源,国内镜像站点,博客文章等

热门文章

  1. 2014 网选 5024 Wang Xifeng's Little Plot
  2. html向上浮动的方式,JS 实现Div向上浮动的实现代码
  3. java 相对路径获取_在java项目中通过相对路径获取资源的方式
  4. 【SSM面向CRUD编程专栏 2】Spring相关API 数据源(连接池)的配置 注解开发 整合junit
  5. SQLPlus命令详细说明
  6. Albert launcher安装与使用
  7. Sublime Text 3 无法输入中文解决方案
  8. BUAA 436 孟竹的复习计划(二维树状数组)
  9. mysql 5.6.4以上版本innodb支持全文索引的测试
  10. 001_docker-compose构建elk环境