In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the differences between Array and List in Scala". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the differences between Array and List in Scala"?
Catalogue
The difference between Scala Array and List
Measurement of List and Array array efficiency of Scala fast row
The difference between Scala Array and List
Difference between Array and List in scala
Q: when do you use Array (Buffer) and List (Buffer)?
List in A:Scala is immutable recursive data (immutable recursive data), which is an infrastructure of Scala. You should use List more than Array (Array is actually mutable, Array of immutable is IndexedSeq).
Mutable Structures
ListBuffer provides a constant time conversion to List.
The Array of a Scala should be generated by Java array, so an Array [Int] may be more efficient than List [Int].
However, I think arrays are used as little as possible in Scala because it feels like you really need to know what's going on underneath to decide whether Array will back up the basic data types you need, or maybe boxed as a wrapper type.
Performance differencesArrayListAccess the ith elementO (1) O (I) Discard the ith elementO (n) O (I) Insert an element at iO (n) O (I) ReverseO (n) O (n) Concatenate (length m) O (n) Calculate the lengthO (1) O (n) memory differencesArrayListGet the first i elementsO (I) O (I) Drop the first i elementsO (n Musti) O (1) Insert an element at iO (n) O (I) ReverseO (n) O (n) Concatenate (length m line n) O (n)
So, unless you need quick random access or count batches of elements, lists are better than arrays.
Measurement of List and Array array efficiency of Scala fast row
Code
Package com.tingfeng.scala.testimport scala.annotation.tailrecimport scala.util. {Random, Sorting} / * * Quick sort Test * / object SortTest {/ * initialize an array Generate random number padding * @ param size * @ return * / def initRandomList (size: Int): list [Int] = {val random = new Random () def initList (size: Int,random: Random): list [Int] = size match {case 0 = > Nil case 1 = > List (random.nextInt ()) case s:Int = > val value = s / 2 if (s% 2 = 0) {initList (value) Random) + + initList (value,random)} else {initList (value,random) + + initList (value + 1MagneRandom)}} initList (size,random)} / * * print out the time used * @ param call * / def printTime (call: = > Unit Tag: String = "") {val startTime = System.currentTimeMillis () println (tag) call println println (s "use time: ${System.currentTimeMillis ()-startTime}\ n")} / * swap the values of two positions in the array It has been tested that this bitwise and method is more efficient than ordinary variable exchange * @ param array * @ param x * @ param y * / def swap (array: Array [Int], x:Int) Y:Int): Unit = {val t = array (x) ^ array (y) array (x) = t ^ array (x) array (y) = t ^ array (y)} / * * return the passed value directly And execute the logic * @ param call * @ param any * @ tparam A * / def doThing [A Unit): a = {call (any) any} / * print list * / def printList [A print (s "$it,")} def shuffleIntSeq (seq: Array [Int] Size: Int): Unit= {val random = new Random () val maxSize = size/2 for (i Nil case head:: other = > val (left, right) = other.partition (_
< head) (qSortList(left) :+ head) ++ qSortList(right) } /** * 通过每次比较数组'head'值与其余值的方式直接实现 * 比'head'小的值移动到其前,比'head'大的移动到其之后 * @param array */ def qSortArray1(array: Array[Int]):Unit = { def sort(ay : Array[Int],start: Int,end: Int):Unit={ if(start >= end) {return} val head = ay (start) var spliteIndex = start for (I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.