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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546140.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一口气说出 6 种延时队列的实现方法,面试官满意的笑了

这是我的第 193 期分享作者 | 程序员内点事来源 | 程序员内点事&#xff08;ID&#xff1a;chegnxy-nds&#xff09; 分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;五一期间原计划是写两篇文章&#xff0c;看一本技术类书籍&#xff0c;结果这五天由于自…

一些c语言图形库

想编程绘制一些简单的图形&#xff0c;有不少的图形库可以选择&#xff1a; &#xff08;1&#xff09;BGI图形库&#xff1a;即turbo c所带的图形库。misaki 在vc&#xff08;vc6,vc2008,vc2010等)下重写了该库&#xff0c;名为EGE 。另一个类似的vc(vc6,vc2008,vc2010)下的库…

acl 服务器编程框架特点介绍

2019独角兽企业重金招聘Python工程师标准>>> acl 中服务器框架模块是一个非常重要的模块&#xff0c;使用该模块技术人员可以快速地写出稳定、安全、高效的网络服务应用&#xff0c;该模块主要来源于著名的邮件服务器程序 (Postfix) 中的 master 模块&#xff0c;为…

turbo c相关文档

无意中在网上找到的turbo c 2.0相关文档&#xff0c;有reference guide 和user guide.下载地址见&#xff08;镜像一 &#xff0c;镜像二 &#xff0c;镜像三 &#xff0c;镜像四 &#xff09;。这些网站还有很多其他各类软件相关文档&#xff0c;感兴趣的可以自己看看。另外&a…

一个类可以有一个接口,接口可以有一个Java类吗?

In the very first step, we will see can a class have an interface in Java? 在第一步中&#xff0c;我们将看到类可以在Java中具有接口吗&#xff1f; Yes, it is possible to define an interface inside the class. 是的&#xff0c;可以在类内部定义接口。 The interf…

人人都能看懂的 6 种限流实现方案!(纯干货)

这是我的第 195 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;为了上班方便&#xff0c;去年我把自己在北郊的房子租出去了&#xff0c;搬到了南郊&#xff0c;这样…

測试新浪微博@小冰 为代码机器人的一些方法

微软的微信小冰被腾讯封杀之后,如今移民到了新浪微博; 小冰 这里贴一些眼下有效的用来识别是这是"机器"而不是有正常人类智商的代码的方法: 1. 在正常的文字中夹杂其他符号,确保不存在有意义的连续的词汇,人眼能够分辨,机器不知所云而会露馅: 比方: ^^^小v冰^^^-…

nethack

nethack是一款开源游戏&#xff0c;不支持声音和华丽的图像&#xff0c;却被称为最好玩的游戏之一。 相关链接&#xff1a; nethack主页 nethack下载地址 nethack wiki nethack贴吧 beginners guide to nethack sources 其他一些有趣的链接&#xff1a; games in c with s…

秒建一个后台管理系统?用这5个开源免费的Java项目就够了

这是我的第 196 期分享作者 | Guide来源 | JavaGuide&#xff08;ID&#xff1a;JavaGuide&#xff09; 分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;大家好&#xff0c;我是 Guide 哥&#xff0c;一个三观比主角还正的技术人。今天推荐几个 Java 项目…

读《白帽子讲Web安全》之客户端脚本安全(一)

2019独角兽企业重金招聘Python工程师标准>>> 【第2章 浏览器安全】 1、同源策略&#xff08;Same Origin Policy&#xff09;是一种约定&#xff0c;它是浏览器最核心也最基本的安全功能。 浏览器的同源策略&#xff0c;限制了来自不同源的“document”或脚本&…

RocketMQ一行代码造成消息发送失败

这是我的第 198 期分享作者 | 丁威来源 | 中间件兴趣圈&#xff08;ID&#xff1a;dingwpmz_zjj&#xff09;分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;1、问题现象首先接到项目反馈使用 RocketMQ 会出现如下错误&#xff1a;错误信息关键点&#xf…

生命游戏(game of life)

生命游戏 &#xff08;game of life )是一款非常著名的游戏。它包括一个二维 矩形世界&#xff0c;这个世界中的每个方格居住着一个活着的或死了的细胞。一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死了的细胞的数量。如果相邻方格活着的细胞数量过多&#xff0c;这…

setpriority_Java Thread类的最终void setPriority(int priority)方法(带示例)

setpriority线程类最终void setPriority(int priority) (Thread Class final void setPriority(int priority)) This method is available in package java.lang.Thread.setPriority(int priority). 软件包java.lang.Thread.setPriority(int priority)中提供了此方法。 This me…

汇编级UART串口初始化与打印

用于新PCB板调试开发&#xff0c;在系统最开始&#xff08;内存初始化之前&#xff09;&#xff0c;尽快打印字符&#xff0c;验证CPU是否正常启动。 以freescale QorIQ 处理器兼容的UART为例&#xff0c;符合16550串口标准&#xff1a; /*UART DEBUG*/ /*#define CCSBAR_RESET…

Java 中的 String 有没有长度限制?

这是我的第 199 期分享作者 | Hollis来源 | Hollis&#xff08;ID&#xff1a;hollischuang&#xff09; 分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;关于String有没有长度限制的问题&#xff0c;我之前单独写过一篇文章分析过&#xff0c;最近我又抽…

c语言编程输入a是输出为a_C ++编程基本输入,输出,数据类型,声明能力倾向问题和解答...

c语言编程输入a是输出为aThis section contains C programming Basic Input, Output, Data types, Declaration etc Aptitude Questions and Answers with explanations. 本节包含C 编程的基本输入&#xff0c;输出&#xff0c;数据类型&#xff0c;声明等&#xff0c;以及有关…

关联数组(associative array)

关联数组&#xff08;associative array )是一种常用的抽象数据类型。它有很多别名&#xff0c;例如associative container , map , mapping , dictionary , finite map , table,index 等。它的特点是由一个关键字和其他各种属性组成的集合。典型的操作包括插入&#xff0c;删除…

开源 免费 java CMS - FreeCMS2.1 菜单管理

2019独角兽企业重金招聘Python工程师标准>>> 项目地址&#xff1a;http://www.freeteam.cn/ 菜单管理 FreeCMS在设计时定位于面向二次开发友好&#xff0c;所以FreeCMS提供了菜单管理功能&#xff0c;二次开发人员可以自由增加新的功能菜单到FreeCMS。 为了让后台…

本来想用“{{”秀一波,结果却导致了内存溢出!

这是我的第 200 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;生活中的尴尬无处不在&#xff0c;有时候你只是想简单的装一把&#xff0c;但某些“老同志”总是在不…

在Ruby中使用&运算符(new_array- arr&old_Array)创建数组实例

In the last articles, we have gone through many methods through which we can create Array Instances but you all must know that those all were Public class methods and now in the upcoming articles, we will be learning about Public instance methods. 在上一篇…