Package Com.dt.scala.list
Object Listbuffer_internals {
def main (args:array[string]) {
Val list = list (1,2,3,4,5,6,7,8,9)
Increment (list)
Increment_moreeffective (list)
Increment_mosteffective (list)
}
def increment (List:list[int]): list[int] = list match {
Case list () = List ()
Case HEAD:: tail = head + 1:: Increment (tail)
}
def increment_moreeffective (List:list[int]): list[int] = {
var result = List[int] ()
for (element <-list) result = Result::: List (element + 1)
Result
}
def increment_mosteffective (List:list[int]): list[int] = {
Import Scala.collection.mutable.ListBuffer
var buffer = new Listbuffer[int]
for (element <-list) Buffer + = element + 1
Buffer.tolist
}
}
3 ways:
1. Stacks that generate too many function calls
2. Many intermediate results have been produced
3. Optimal
Information from DT Big Data Dream Factory, public number: Dt_spark
Video address:http://edu.51cto.com/lesson/id-71305.html
Scala's classic 82nd: How does the listbuffer of the list in Scala implement efficient traversal calculations?