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 elementAdc.delete_at(ind)# printing the arrayputs "Array elements after remove operation:"puts Adc elseputs "Index is not valid" endOutput
輸出量
Enter the index of element you want to delete:3 Array elements after remove operation: Includehelp.com Ruby C++ Java PythonExplanation:
說明:
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 AdcOutput
輸出量
RUN 1: Enter the element you want to delete:C++ Array elements after remove operation: Includehelp.com Ruby C# Java PythonRUN 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中移除元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: math.trunc_带有Python示
- 下一篇: synchronized 中的 4 个优