📜  Scala List sum() 方法与示例

📅  最后修改于: 2022-05-13 01:55:26.574000             🧑  作者: Mango

Scala List sum() 方法与示例

sum()方法用于添加所述列表的所有元素。

示例 #1:

// Scala program of sum()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a list
        val m1 = List(1, 2, 3, 4, 5, 7)
          
        // Applying sum method
        val result = m1.sum
          
        // Displays output
        println(result)
      
    }
}
输出:
22

示例 #2:

// Scala program of sum()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a list
        val m1 = List(1, 0)
          
        // Applying sum method
        val result = m1.sum
          
        // Displays output
        println(result)
      
    }
}
输出:
1