在Ruby中使用&运算符(new_array- arr&old_Array)创建数组实例
In the last articles, we have gone through many methods through which we can create Array Instances but you all must know that those all were Public class methods and now in the upcoming articles, we will be learning about Public instance methods.
在上一篇文章中,我們介紹了許多可以創(chuàng)建數(shù)組實(shí)例的方法,但是您必須都知道它們都是Public類方法,現(xiàn)在在接下來的文章中,我們將學(xué)習(xí)Public實(shí)例方法。
Now, you all must be thinking what is the difference between Public class methods and Public instance methods, then to clear your doubt, Public class methods can be invoked as soon as Ruby processes the execution of the class whereas Public instance methods can only be executed after the creation of an Instance of that particular class. 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 & operator.
現(xiàn)在,您必須都在考慮Public類方法和Public實(shí)例方法之間的區(qū)別,然后為澄清您的疑問,只要Ruby處理類的執(zhí)行,就可以調(diào)用Public類方法,而Public實(shí)例方法只能執(zhí)行在創(chuàng)建該特定類的實(shí)例之后。 我們研究了Array.new()之類的方法,該方法是Public類方法的居民,但現(xiàn)在將研究Public實(shí)例方法。 好吧,在本文中,我們將研究如何在&運(yùn)算符的幫助下創(chuàng)建Array實(shí)例 。
Method description:
方法說明:
The & symbol is the way to create an Array object and it requires two previously defined Array instances. It is also known as the Set intersection. It works in the way that it returns a new Array instance having elements that are common to both the Arrays, excluding the duplicate elements present in them. The order is maintained from the original Array.
&符號是創(chuàng)建Array對象的方法,它需要兩個(gè)先前定義的Array實(shí)例。 也稱為Set相交。 它的工作方式是返回一個(gè)新的Array實(shí)例,該實(shí)例具有兩個(gè)數(shù)組共同的元素,但不包括其中存在的重復(fù)元素。 該順序是從原始Array維護(hù)的。
Syntax:
句法:
aew_array = old_array1 & old_array2Parameter(s):
參數(shù):
The & operator takes two arguments and they both are Array instances.
&運(yùn)算符帶有兩個(gè)參數(shù),它們都是Array實(shí)例。
Example 1:
范例1:
=beginRuby program to create Array with & =end# arrays old_arr1 = [23,44,66,33,12,77,33] old_arr2 = [12,23,66,2,44,77,8]# creating a new array using & operator new_Arr = old_arr1 & old_arr2# printing the array puts "The new Integer Array Instance is:" print new_ArrOutput
輸出量
The new Integer Array Instance is: [23, 44, 66, 12, 77]Explanation:
說明:
In the above code, you can observe that we are creating an Array instance with the help of & operator. In the resulting Array, you can see that all the elements are residing which common in both the Array are being passed as the arguments.
在上面的代碼中,您可以看到我們正在使用&運(yùn)算符創(chuàng)建一個(gè)Array實(shí)例 。 在生成的Array中,您可以看到所有元素都駐留在兩個(gè)Array中哪個(gè)公共位置作為參數(shù)傳遞。
Example 2:
范例2:
=beginRuby program to create Array with & operator =end# arrays old_arr1 = ['Ramit','Amit','Suresh','Payal','Samir'] old_arr2 = ['Babita','Amit','Suresh']# creating a new array using & operator new_Arr = old_arr1 & old_arr2# printing the array puts "The new String Array Instance is:" print new_ArrOutput
輸出量
The new String Array Instance is: ["Amit", "Suresh"]Explanation:
說明:
In the above code, you can observe that we are creating a String Array with the help of & operator. This operator internally uses .eql? method for faster carrying out a faster checking of Array elements of both the Arrays.
在上面的代碼中,您可以觀察到我們在&運(yùn)算符的幫助下創(chuàng)建了一個(gè)字符串?dāng)?shù)組 。 該運(yùn)算符內(nèi)部使用.eql嗎? 快速執(zhí)行兩個(gè)數(shù)組的數(shù)組元素的快速檢查的方法 。
翻譯自: https://www.includehelp.com/ruby/creating-array-instance-with-and-operator-new_array-arr-old_array.aspx
總結(jié)
以上是生活随笔為你收集整理的在Ruby中使用&运算符(new_array- arr&old_Array)创建数组实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: scala 字符串占位符_如何在Scal
- 下一篇: java对数组进行排序_用Java对数组