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.pop和Array.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 AdcOutput
輸出量
Array elements before removing... Includehelp.com Ruby C++ C# Java Python Array elements after removing... Includehelp.com Ruby C++ C# JavaExplanation:
說明:
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 AdcOutput
輸出量
Array elements before removing... Includehelp.com Ruby C++ C# Java Python Array elements after removing... Ruby C++ C# Java PythonExplanation:
說明:
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
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的ruby array_在Ruby中使用Array.pop和Array.shift方法从Array中删除元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java后台json传递,后台json传
- 下一篇: c语言中的运算符及其含义_按位运算符及其