当先锋百科网

首页 1 2 3 4 5 6 7

arr[-1,-1]

In the last article, we have gone through the method by which we can create Array instance with the help of + operator. You all must know that in the articles which were related to Array creation are Public class methods and now in most of the previous articles, we have learned about Public instance methods. For a quick overview, we have studied & operator, * operator, etc. We also know that what is the difference between Public Instance methods and Public class methods. We have studied methods like Array.new() which is a resident of Public class methods but now will be studying Public instance methods. Well, in this article, we will be studying about creating the Array instance with the help of the - operator.

在上一篇文章中,我们介绍了可以在+运算符的帮助下创建Array实例的方法。 大家都必须知道,在与Array创建相关的文章中,都是Public类方法,现在在前面的大多数文章中,我们已经了解了Public实例方法。 为了快速浏览,我们研究了&运算符*运算符等。我们还知道Public Instance方法和Public类方法之间的区别是什么。 我们研究了Array.new()之类的方法,该方法是Public类方法的居民,但现在将研究Public实例方法。 好吧,在本文中,我们将研究如何在-运算符的帮助下创建Array实例

Method description:

方法说明:

This is one of the ways through which you can generate new Array. It works in the way that it returns the copy of the original Array that is the copy of the first Array and this copy does not contain the elements which are present in the second Array. The order which is followed in the new Array is dependent upon the Original Array. This method uses .eql method for efficient processing.

这是生成新数组的方式之一。 它的工作方式是返回原始数组的副本(即第一个数组的副本),并且此副本不包含第二个数组中存在的元素。 新阵列中遵循的顺序取决于原始阵列。 此方法使用.eql方法进行有效处理。

Parameter(s):

参数:

The '-' operator takes two arguments. The first one is the previously defined Array and the second one is another Array.

“-”运算符采用两个参数。 第一个是先前定义的数组,第二个是另一个数组。

Example 1:

范例1:

=begin
  Ruby program to create Array with - operator
=end

# arrays
old_arr1 = [23,44,66,889]
old_arr2 = [33,56,22,23]

# creating a new array 
new_arr = old_arr1 - old_arr2

# printing the array
puts "The new Integer Array Instance is: "
print new_arr

Output

输出量

The new Integer Array Instance is: 
[44, 66, 889]

Explanation:

说明:

In the above code, you can observe that we are creating an Array instance with the help of the - operator. In the resulting Array, you can see that the new Array is not containing elements that are present in the second Array.

在上面的代码中,您可以观察到我们正在使用-运算符创建一个Array实例 。 在生成的数组中,您可以看到新数组不包含第二个数组中存在的元素。

Example 2:

范例2:

=begin
  Ruby program to create Array with - operator
=end

# arrays
old_arr1 = ['Ramit','Amit','Suresh','Payal']
old_arr2 = ['Payal','Samir','Sonakshi','Hira','Panna']

# creating a new array 
new_arr = old_arr1 - old_arr2

# printing the array 
puts "The new String Array Instance is: "
print new_arr

Output

输出量

The new String Array Instance is: 
["Ramit", "Amit", "Suresh"]

Explanation:

说明:

In the above code, you can observe that we are creating a String Array with the help of the - operator. The resulting Array is having all the Strings which are present in the first Array but not in the second Array.

在上面的代码中,您可以观察到我们正在使用-运算符创建一个字符串数组 。 结果数组具有所有出现在第一个数组中但不存在于第二个数组中的字符串。

翻译自: https://www.includehelp.com/ruby/creating-array-instance-with-minus-operator-new_array-arr-old_array.aspx

arr[-1,-1]