ruby array

Ruby Array.delete()和Array.delete_at()方法 (Ruby Array.delete() and Array.delete_at() methods)

In the last article, we have seen how we can remove the elements from an instance of Array class with the help of Array.pop() and Array.shift()?

在上一篇文章中,我们看到了如何借助Array.pop()和Array.shift()从Array类的实例中删除元素 ?

Those were the two methods that do not take any arguments. For a quick revision, let me remind you that Array.pop() removes the element from the backside or from the end of the Array so there is no need to pass any argument as whichever the element is found by the method as the last one will be removed. The same goes for the Array.shift() method. It is totally opposite of the Array.pop() and removes the article from the front of the Array. Here also, you don't need to pass any argument because whichever element is found by the method as the first element will be removed. In the article, we will discuss two more such methods which will help us to remove the elements from the Array and they are namely Array.delete() and Array.delete_at(). We will learn how to implement them with the help of their syntaxes and their supporting examples.

这是两个不带任何参数的方法。 为了快速进行修订,让我提醒您, Array.pop()从Array的背面或末端删除了元素,因此无需传递任何参数,因为方法找到的最后一个元素将被删除。 Array.shift()方法也是如此 。 它与Array.pop()完全相反,并从Array的前面删除了该文章。 同样在这里,您不需要传递任何参数,因为该方法找到的任何元素都将被删除,因为第一个元素将被删除。 在本文中,我们将讨论另外两个这样的方法,它们将帮助我们从Array中删除元素,即Array.delete()和Array.delete_at() 。 我们将学习如何借助其语法和支持示例来实现它们。

使用Array.delete_at()方法从Array中删除元素 (Removing elements from Array using Array.delete_at() method)

In Array.delete_at() method, we have to pass the index of the element we want to delete from the instance of Array class. The interpreter will not give an error if you will provide an index that is not available in the Array. Instead, it will give you the null if the provided index is not found.

Array.delete_at()方法中 ,我们必须传递要从Array类的实例中删除的元素的索引 。 如果您将提供数组中不可用的索引,则解释器不会给出错误。 相反,如果找不到提供的索引 ,它将为您提供null

Syntax:

句法:

    Array.delete_at(index)

Here, index represents the position of the element in the Array to be deleted.

在这里, index表示要删除的元素在Array中的位置。

Program:

程序:

=begin
Ruby program to remove elements from Array using
Array.delete_at() method
=end
# Array declaration
Adc = ['Includehelp.com','Ruby','C++','C#','Java','Python']
# input the index/position
puts "Enter the index of element you want to delete:"
ind = gets.chomp.to_i
# checking the index bound and removing
# the element
if(ind>=0 && ind<Adc.count)
# removing the element
Adc.delete_at(ind)
# printing the array
puts "Array elements after remove operation:"
puts Adc
else
puts "Index is not valid"
end

Output

输出量

Enter the index of element you want to delete:
3
Array elements after remove operation:
Includehelp.com
Ruby
C++
Java
Python

Explanation:

说明:

In the above code, you can observe that we are taking input from the user and that input is nothing but the index of the element which the user wants to remove from the Array. We are proceeding with the help of the Array.delete_at() method which is removing the element.

在上面的代码中,您可以观察到我们正在从用户那里获取输入,而该输入仅是用户想要从Array中删除的元素的索引。 我们正在使用Array.delete_at()方法的帮助来删除元素。

使用Array.delete()方法从Array中删除元素 (Removing elements from Array using Array.delete() method)

As the name suggests, Array.delete() will delete all the elements present in the array which are having the same as passed in the method. Alike Array.delete_at(), this method doesn't work with the help of index instead we need to pass the element name inside this method.

顾名思义, Array.delete()将删除数组存在的所有与方法中传递的元素相同的元素 。 与Array.delete_at()类似 ,此方法在index的帮助下不起作用,而是需要在此方法中传递元素名称。

Syntax:

句法:

    Array.delete(element)

Here, element represents the element in the Array to be deleted. (Note: It will search the availability of that element and will delete all the same name element from the Array.)

在这里, element表示要删除的Array中的元素。 ( 注意:它将搜索该元素的可用性,并将所有相同名称的元素从Array中删除。)

Program:

程序:

=begin
Ruby program to remove elements from Array using
Array.delete
=end
# array declaration
Adc = ['Includehelp.com','Ruby','C++','C#','Java','Python','C++']
# input the element
puts "Enter the element you want to delete:"
element = gets.chomp
# removing the element
Adc.delete(element)
# printing the element
puts "Array elements after remove operation:"
puts Adc

Output

输出量

RUN 1:
Enter the element you want to delete:
C++
Array elements after remove operation:
Includehelp.com
Ruby
C#
Java
Python
RUN 2:
Enter the element you want to delete:
Perl
Array elements after remove operation:
Includehelp.com
Ruby
C++
C#
Java
Python
C++

Explanation:

说明:

In the above code and output, you can observe that the user has entered "C++" as the argument and all the elements named "C++" got deleted from the Array. In RUN 2, you can observe that the method imposes no effect if the element is not found in the Array.

在上面的代码和输出中,您可以看到用户输入了“ C ++”作为参数,并且所有名为“ C ++”的元素都已从数组中删除。 在RUN 2中 ,您可以观察到,如果在Array中找不到元素,则该方法不起作用。

翻译自: https://www.includehelp.com/ruby/removing-elements-from-array-using-array-delete-and-array-delete_at.aspx

ruby array

ruby array_在Ruby中使用Array.delete()和Array.delete_at()从Array中移除元素相关推荐

  1. ruby array_在Ruby中使用Array.pop和Array.shift方法从Array中删除元素

    ruby array Ruby Array.pop和Array.shift方法 (Ruby Array.pop and Array.shift methods) If you are reading ...

  2. oracle中drop、delete和truncate的区别

    oracle中drop.delete和truncate的区别 oracle中可以使用drop.delete和truncate三个命令来删除数据库中的表,网上有许多文章和教程专门讲解了它们之间的异同,我 ...

  3. _.uniq_在Ruby中使用Array.compact和Array.uniq方法从Array中移除元素

    _.uniq Ruby Array.compact和Array.uniq方法 (Ruby Array.compact and Array.uniq Methods) In the last artic ...

  4. c++堆内存默认大小_C++|array new 和 array delete的堆内存细节

    在C中, 堆内存的申请与释放通过malloc()和free()来进行,而对于C++,其运算符函数new.delete不但封装了malloc()和free(),还增加了一些额外的操作,来得更安全和便捷. ...

  5. centos下安装ruby,删除ruby

    简言 由于centos7下面,直接使用yum install ruby,安装的是2.0.0版本,太低了,没法部署redis集群,redis集群需要2.3版本,所以我们需要安装更高的版本 注意 在新版r ...

  6. 从自己实现Ruby单例模式揭秘Ruby模块内幕

         从自己实现Ruby单例模式揭秘Ruby模块内幕 缘起 整个故事要从某一天开始说起,那天,我看到一篇文章介绍了Ruby中实现单例模式的模块.Singleton模块,只要include  Sin ...

  7. 浅谈 C++ 中的 new/delete 和 new[]/delete[]

    来自:http://www.cnblogs.com/hazir/p/new_and_delete.html 在 C++ 中,你也许经常使用 new 和 delete 来动态申请和释放内存,但你可曾想过 ...

  8. C++ 中的 new/delete 和 new[]/delete[]深入理解

    在 C++ 中,你也许经常使用 new 和 delete 来动态申请和释放内存,但你可曾想过以下问题呢? new 和 delete 是函数吗? new [] 和 delete [] 又是什么?什么时候 ...

  9. 谈谈 C++ 中的 new/delete 和 new[]/delete[]

    在 C++ 中,你也许经常使用 new 和 delete 来动态申请和释放内存,但你可曾想过以下问题呢? new 和 delete 是函数吗? new [] 和 delete [] 又是什么?什么时候 ...

最新文章

  1. 移动端重构系列5——等分,居中等
  2. mySQL之单表更新
  3. 网络配置及shell基础
  4. 进度条设置_项目功能分解4:MATLAB GUI如何设计有特色的进度条。
  5. Scikit-learn数据预处理分类变量编码之标签二值化
  6. 算法 --- 反转数组
  7. 「PowerBI」使用TabularEditor进行PowerBIDeskTop模型开发最佳实践
  8. 这才是JAVA中打印日志的正确姿势
  9. 数据结构排序系列详解之二 希尔排序
  10. python单元测试框架作用_Python单元测试框架:Pytest简介
  11. (二)MR之reduce多目录输出
  12. Redhat5.2yum源更新为centos源
  13. Oracle with..as使用方法
  14. Java爬虫需要的包_java爬虫需要的jar包
  15. IP雷达4.0 测试版
  16. 深度卷积神经网络(一)
  17. 一文读懂AB测试原理及样本量计算的Python实现
  18. 通过 purge_relay_logs 自动清理relaylog
  19. ssm学生请假系统java学生请假系统源码
  20. 推荐模型-上下文感知-2016:FNN模型【FM家族】【FM+MLP=FNN】

热门文章

  1. 离人愁有用计算机怎么写歌词,离人愁歌词是什么意思 今两股痒痒什么意思
  2. mysql高级查询教程_MYSQL高级查询
  3. ubunt16.04 安装3090显卡驱动 cuda cudnn pytorch
  4. uint8 转换为 float
  5. 沟通linux与windows的wine
  6. Kewail-邮件短信接口的基础教程
  7. 高效管理论坛广告贴的小窍门
  8. 二分排序java实现
  9. Spring Security源码分析四:Spring Social实现微信社交登录
  10. Centos 7安装与配置nagios监控(一)