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 an article that is related to deleting elements from the instance of Array class then it is expected from you that you are aware of the basic things related to Array such as how to declare it and how to find the size of the Array. If you don't know this basic information then you can read related articles and gather the information which is available at includehelp.com/ruby/. In this article, you will come to know about various methods that are available in Ruby's library specifically defined to delete the elements from the Array. We are very well aware of the fact that Ruby's arrays are dynamic in nature which means that we can store n number of elements in them without taking care or bothering about their predefined size. Ruby facilitates you with different methods through which you can remove or delete elements from the object of the Array class. Well, this article we will be discussing two of them which are namely Array.pop and Array.shift.

如果您正在阅读与从Array类的实例中删除元素有关的文章,那么您应该了解与Array有关的基本知识,例如如何声明它以及如何找到Array的大小。 。 如果您不了解此基本信息,则可以阅读相关文章并收集包含在includehelp.com/ruby/中的信息。 在本文中,您将了解Ruby的库中可用的各种方法,这些方法专门定义用于从Array中删除元素。 我们非常清楚Ruby的数组本质上是动态的,这意味着我们可以在其中存储n个元素,而无需担心或打扰它们的预定义大小。 Ruby提供了多种方法来帮助您,通过这些方法可以从Array类的对象中删除或删除元素。 好吧,本文我们将讨论其中两个,即Array.popArray.shift

1)使用Array.pop方法删除元素 (1) Deleting elements by using Array.pop method)

Array.pop method will delete elements from the right-hand side of the Array or you can say that it removes elements from the end of the backside. If you are using Array.pop one time it means that you want to remove the last element of the Array.

Array.pop方法将从Array的右侧删除元素,或者可以说它从背面的末端删除了元素。 如果您一次使用Array.pop,则意味着您要删除Array的最后一个元素。

Program:

程序:

=begin
Ruby program to remove elements from Array 
using Array.pop	
=end

# declaring an array 
Adc = ['Includehelp.com','Ruby','C++','C#','Java','Python']

# printing array elements 
puts "Array elements before removing..."
puts Adc

# calling Array.pop method to remove element
Adc.pop

# printing array elements 
puts "Array elements after removing..."
puts Adc

Output

输出量

Array elements before removing...
Includehelp.com
Ruby
C++
C#
Java
Python
Array elements after removing...
Includehelp.com
Ruby
C++
C#
Java

Explanation:

说明:

In the above code and its output, you can observe that Array.pop is removing the elements from the end of the Array. The element "python" is removed from the Array.

在上面的代码及其输出中,您可以观察到Array.pop正在删除Array末尾的元素。 从数组中删除了元素“ python”

2)使用Array.shift方法删除元素 (2) Deleting elements by using Array.shift method)

It is just the opposite of Array.pop. Array.shift method will delete elements from the left-hand side of the Array or you can say that it removes elements from the beginning of the front side. If you are using Array.shift one time then it means that you want to remove the first element of the Array.

它与Array.pop相反。 Array.shift方法将从Array的左侧删除元素,或者可以说它从正面的开头删除了元素。 如果您一次使用Array.shift,则意味着您要删除Array的第一个元素。

Program:

程序:

=begin
Ruby program to remove elements from Array 
using Array.shift
=end

# declaring an array 
Adc = ['Includehelp.com','Ruby','C++','C#','Java','Python']

# printing array elements 
puts "Array elements before removing..."
puts Adc

# calling Array.shift method to remove element
Adc.shift

# printing array elements 
puts "Array elements after removing..."
puts Adc

Output

输出量

Array elements before removing...
Includehelp.com
Ruby
C++
C#
Java
Python
Array elements after removing...
Ruby
C++
C#
Java
Python

Explanation:

说明:

In the above code and its output, you can observe that Array.shift is removing the elements from the beginning of the Array. The element "Includehelp.com" is removed from the Array.

在上面的代码及其输出中,您可以观察到Array.shift正在从Array的开头删除元素。 元素“ Includehelp.com”已从数组中删除。

翻译自: https://www.includehelp.com/ruby/deleting-elements-from-array-using-array-pop-and-array-shift.aspx

ruby array

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值