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