當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Scala Array &()用法及代碼示例


Array#&():&() 是一個 Array 類方法,它返回一個新數組,其中包含兩個數組共有的唯一元素。

用法:Array.&()

參數:用於比較的數組

Return: 包含兩個數組共有的唯一元素的新數組

示例#1:


# Ruby code for &() method  
     
# declaring arrays  
a = [18, 22, 33, 4, 5, 6]  
     
# declaring arrays  
b = [18, 22, 33, 4, 5, 6]  
     
# declaring arrays  
c = [18, 22, 33, 40, 50, 6]  
     
# & method 
puts "& method form : #{a & b}\n\n"
     
# & method 
puts "& method form : #{a & c}\n\n"
     
# & method  
puts "& method form : #{b & c}\n\n"

輸出:

& method form : [18, 22, 33, 4, 5, 6]

& method form : [18, 22, 33, 6]

& method form : [18, 22, 33, 6]

示例#2:


# Ruby code for &() method  
     
# declaring arrays  
a = ["abc", "xyz", "dog"]  
     
# declaring arrays  
b = ["cat", "cat", "dog"]  
     
# declaring arrays  
c = ["cat", "cat", "dog"]  
     
# & method 
puts "& method form : #{a & b}\n\n"
     
# & method 
puts "& method form : #{a & c}\n\n"
     
# & method 
puts "& method form : #{b & c}\n\n"

輸出:

& method form : ["dog"]

& method form : ["dog"]

& method form : ["cat", "dog"]



相關用法


注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | Array &() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。