apply()方法用於通過列表中的索引選擇元素。
函數定義: def apply(n: Int): A
返回類型: It returns an element from the given list by its index which is present in the apply method as argument.
範例1:
// Scala program of apply()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = List(1, 3, 5, 2)
// Applying apply method
val res = m1.apply(3)
// Displays output
println(res)
}
}
輸出:
2
範例2:
// Scala program of apply()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = List(1, 3, 5, 2)
// Applying apply method
val res = m1.apply(0)
// Displays output
println(res)
}
}
輸出:
1
相關用法
- Scala Set apply()用法及代碼示例
- Scala Map apply()用法及代碼示例
- Scala TreeSet apply()用法及代碼示例
- Scala SortedSet apply()用法及代碼示例
- Scala Queue apply()用法及代碼示例
- Scala SortedMap apply()用法及代碼示例
- Scala Stack apply()用法及代碼示例
- Scala Listset apply()用法及代碼示例
- Scala List sum()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Scala List apply() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。