Scala-Array, Mapping

Keywords: Scala Java

1, array
1) Create arrays
Create fixed-length arrays and variable-length arrays

//Create a fixed-length array, use the new keyword, specify that the array generic type is Int, the length is 5, the default will initialize these five values for the default value of the specified type, such as Int, then the five default values are all 0.
//Note: If new, it's equivalent to calling the array's apply method to assign values directly to the array
scala> val arr = new Array[Int](5)
arr: Array[Int] = Array(0, 0, 0, 0, 0)
//Modify the values of array elements
scala> arr(0)
res16: Int = 0

scala> arr(0) = 1

scala> arr(1) = 2

scala> arr(2) = 3

scala> arr
res20: Array[Int] = Array(1, 2, 3, 0, 0)

//Direct creation of fixed-length arrays and initialization of elements
scala> val arr2 = Array(1,2,3,4,5)
arr2: Array[Int] = Array(1, 2, 3, 4, 5)

scala> arr2
res21: Array[Int] = Array(1, 2, 3, 4, 5)

//Print arrays, by default hashcode, and toBuffer prints array elements
scala> println(arr)
[I@6c06b1bc

scala> println(arr.toBuffer)
ArrayBuffer(1, 2, 3, 0, 0)

//Note the following because there is a difference between using new
scala> val a = Array[Int](10)
a: Array[Int] = Array(10)
a.length = 1
scala> val b = new Array[Int](10)
b: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
a.length = 10
//Create Array Buffer as an Array (using the Array Buffer cache)
scala> val ab = new ArrayBuffer[Int]()
//The prompt is earlier than the ArrayBuffer type and needs a guide.
<console>:7: error: not found: type ArrayBuffer
       val ab = new ArrayBuffer[Int]()
                    ^
//Press tab key to give a prompt
scala> import scala.collection.mutable.Array
ArrayBuffer    ArrayBuilder   ArrayLike      ArrayOps       ArraySeq       ArrayStack

//Package completion
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

//Create a variable-length array of new ArrayBuffer[Int] () or do not require a new keyword
ArrayBuffer[Int](),Now that it's getting longer, there's no need to specify the length.
scala> val ab = new ArrayBuffer[Int]()
ab: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

//Adding one or more elements to an array+=

scala> ab += 1
res24: ab.type = ArrayBuffer(1)

scala> ab += 2
res25: ab.type = ArrayBuffer(1, 2)

//Add multiple elements
scala> ab += (3,4,5)
res26: ab.type = ArrayBuffer(1, 2, 3, 4, 5)

scala> ab
res27: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)

2) Array operation

//Array additive variable length and fixed length can also be used together.++=
//Two variable-length arrays are added, and the elements in ac are added to ab
scala> ab ++= ac
res32: ab.type = ArrayBuffer(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)

scala> ab
res33: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)

//Add fixed-length array elements to variable-length array ac
scala> ac ++= Array(6,7,8)
res34: ac.type = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8)

scala> ac
res35: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8)

//Insert elements at specified positions in the array, using insert
//Insert two 9 at the corner of the ac array labeled 1
scala> ac.insert(1,9,9)

scala> ac
res38: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 9, 9, 2, 3, 4, 5, 6, 7, 8)

//Insert two 9 into the position marked 0 by ac angle
scala> ac.insert(0,9,9)

scala> ac
res40: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(9, 9, 1, 9, 9, 2, 3, 4, 5, 6, 7, 8)

//Delete the element at the specified location of the array, using remove
//Delete 2 elements from the position marked 0 by ac
scala> ac.remove(0,2)

scala> ac
res42: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 9, 9, 2, 3, 4, 5, 6, 7, 8)

scala> ac.remove(1,2)

scala> ac
res44: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8)

3) traversal array
Use the for loop and until
Useful until generates footmarks, 0 until 10 contains 0 and does not contain 10

scala> 0 until 10
res47: scala.collection.immutable.Range = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

//Variable array
scala> for(i <- 0 until ac.length) print (ac(i) + " ")
1 2 3 4 5 6 7 8

4) Array Conversion
The field keyword converts the original array to produce a new array, with the original array unchanged

scala> val arr = Array(1,2,3,4,5,6,7,8,9)
arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)

//Put each element in arr multiplied by 10 into a new array
scala> val arr2 = for (i <- arr) yield i * 10
arr2: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90)

scala> arr2
res0: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90)

//It's more convenient to use map
scala> val arr3 = arr.map(_ * 10)
arr3: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90)

scala> arr3
res2: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90)
//perhaps
scala> val arr4 = arr.map(x => x * 10)
arr4: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90)

Use filters

scala> val arr2 = for( i <- arr if i % 2 == 0) yield i * 2
arr2: Array[Int] = Array(4, 8, 12, 16)
//It's not good to use map directly for the above situation, because there are filtering conditions, so filter is used here.
scala> val arr5 = arr.filter(x => x % 2 ==0)
arr5: Array[Int] = Array(2, 4, 6, 8)

scala> val arr5 = arr.filter(x => x % 2 ==0).map(x => x * 2)
arr5: Array[Int] = Array(4, 8, 12, 16)

scala> arr5
res6: Array[Int] = Array(4, 8, 12, 16)

//Or use as a simpler way of writing
scala> val arr6 = arr.filter(_ % 2 ==0).map(_ * 2)
arr6: Array[Int] = Array(4, 8, 12, 16)

scala> arr6
res7: Array[Int] = Array(4, 8, 12, 16)

5) Array Common Algorithms

//Array maximum, minimum, sum
scala> arr.sum
res18: Int = 41

scala> arr.max
res19: Int = 9

scala> arr.min
res20: Int = 1

//Some Methods of Array Sorting
scala> val arr = Array(3,1,6,8,5,4,9,2,3)
arr: Array[Int] = Array(3, 1, 6, 8, 5, 4, 9, 2, 3)

scala> arr.sorted
res9: Array[Int] = Array(1, 2, 3, 3, 4, 5, 6, 8, 9)

scala> arr
res10: Array[Int] = Array(3, 1, 6, 8, 5, 4, 9, 2, 3)

scala> val arr2 = arr.sorted
arr2: Array[Int] = Array(1, 2, 3, 3, 4, 5, 6, 8, 9)

scala> arr2
res11: Array[Int] = Array(1, 2, 3, 3, 4, 5, 6, 8, 9)

scala> val arr2 = arr.sorted.reverse
arr2: Array[Int] = Array(9, 8, 6, 5, 4, 3, 3, 2, 1)

scala> arr2
res12: Array[Int] = Array(9, 8, 6, 5, 4, 3, 3, 2, 1)

scala> arr.sortBy(x => x)
res13: Array[Int] = Array(1, 2, 3, 3, 4, 5, 6, 8, 9)

scala> arr.sortWith(_>_)
res14: Array[Int] = Array(9, 8, 6, 5, 4, 3, 3, 2, 1)

scala> arr.sortWith(_<_)
res15: Array[Int] = Array(1, 2, 3, 3, 4, 5, 6, 8, 9)

scala> arr.sortWith((x,y)=> x<y)
res16: Array[Int] = Array(1, 2, 3, 3, 4, 5, 6, 8, 9)

scala> arr.sortWith((x,y)=> x>y)
res17: Array[Int] = Array(9, 8, 6, 5, 4, 3, 3, 2, 1)

2, mapping
In Scala, the data structure of hash tables is called Mapping

1) Create mappings

//The first way to create it is to use "->" a - > b as if a is the key B is the value
scala> val props = Map(1 -> 23,2 -> 45)
//In the case of java, both key and value types are Int
props: scala.collection.immutable.Map[Int,Int] = Map(1 -> 23, 2 -> 45)
scala> val props = Map("user" -> "admin", "pwd" -> "1234")
//In java, both key and value types are String
props: scala.collection.immutable.Map[String,String] = Map(user -> admin, pwd -> 1234)

//The second way to create it is to use (k,v)
scala> val map = Map((1,"tom"),(2,"jack"))
map: scala.collection.immutable.Map[Int,String] = Map(1 -> tom, 2 -> jack)

scala> val name = map(1)
name: String = tom

2. Getting and modifying values in maps

//Value by key
scala> val name = map(1)
name: String = tom
//Modified values are not found, because the Map above is immutable and needs to be re-imported.
scala> map(1) = "lucy"
<console>:9: error: value update is not a member of scala.collection.immutable.Map[Int,String]
              map(1) = "lucy"

//Re-importing map packages with variable values can modify elements successfully
scala> import scala.collection.mutable.Map
Map          MapBuilder   MapLike      MapProxy

scala> import scala.collection.mutable.Map
import scala.collection.mutable.Map

scala> val map = Map((1,"tom"),(2,"jack"))
map: scala.collection.mutable.Map[Int,String] = Map(2 -> jack, 1 -> tom)

scala> map(1) = "lucy"

scala> map
res25: scala.collection.mutable.Map[Int,String] = Map(2 -> jack, 1 -> lucy)

//Adding elements to maps
scala> map += ((3,"wangw"),(4,"lining"))
res26: map.type = Map(2 -> jack, 4 -> lining, 1 -> lucy, 3 -> wangw)

scala> map += (5 -> "xiaox",6 -> "jiej")
res27: map.type = Map(2 -> jack, 5 -> xiaox, 4 -> lining, 1 -> lucy, 3 -> wangw, 6 -> jiej)

//In addition, if k is the same, then v will be overwritten, and the corresponding xiaox of hair 5 will be modified to xiao.
scala> map += (5 -> "xiao")
res28: map.type = Map(2 -> jack, 5 -> xiao, 4 -> lining, 1 -> lucy, 3 -> wangw, 6 -> jiej)

//Addition of two maps++=
scala> val map2 = Map((1,"laowang"),(2,"lisi"))
map2: scala.collection.mutable.Map[Int,String] = Map(2 -> lisi, 1 -> laowang)

scala> val map3 = Map(3 ->"jinbm")
map3: scala.collection.mutable.Map[Int,String] = Map(3 -> jinbm)

//When added, if k is the same, v will cover, and the elements in map 3 will be added to map 2.
scala> map2 ++= map3
res33: map2.type = Map(2 -> lisi, 1 -> laowang, 3 -> jinbm)

scala> map2
res34: scala.collection.mutable.Map[Int,String] = Map(2 -> lisi, 1 -> laowang, 3 -> jinbm)

Note: If you can't get v based on k in the mapping, you will report an error.

scala> map2(4)
java.util.NoSuchElementException: key not found: 4
        at scala.collection.MapLike$class.default(MapLike.scala:228)
        at scala.collection.AbstractMap.default(Map.scala:58)
        ...

//Use getOrElse to solve this problem. If you can't find the v corresponding to this k, give it a default value.
scala> map2.getOrElse(4,0)
res36: Any = 0

Print Narcissus Number

(100 to 999).filter(x => {pow(x%10,3) + pow(x/10%10,3) + pow(x/100,3) == x}).foreach(println)



 

Posted by helppy on Fri, 29 Mar 2019 23:57:30 -0700