码迷,mamicode.com
首页 > 编程语言 > 详细

Scala--数组类型

时间:2019-11-08 20:56:33      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:reducer   exist   filter   rem   pen   rev   clone   element   dex   

1、定长数组Array

scala> val a = new Array[Int](10)
a: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

Int类型的数组的默认值是0

---------------------------------------------------------------

scala> val a = new Array[String](10)
a: Array[String] = Array(null, null, null, null, null, null, null, null, null, null)

//String类型的数组的默认值是null

---------------------------------------------------------------

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

-----------------------------------------------------------------

scala> val a = Array("Tom","Jack","Anny")
a: Array[String] = Array(Tom, Jack, Anny)

--------------------------------------------------------------

scala> val a = Array("Tom","Jack","Anny",4)
a: Array[Any] = Array(Tom, Jack, Anny, 4)

//这个里面的Any相当于Java里面的object类型

---------------------------------------------------------------

scala> val a:Arrauy[String] = Array("Tom","Jack","Anny",4)
<console>:11: error: not found: type Arrauy
val a:Arrauy[String] = Array("Tom","Jack","Anny",4)

//这个里面的数组类型是String类型的,所以数组里面包含的Int类型的值是不能这么声明的

 

=========================================

2、变长数组ArrayBuffer

scala> import scala.collection.mutable._
import scala.collection.mutable._

scala> val a = ArrayBuffer[Int]()
a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

//想要使用ArrayBuffer必须先要导入scala.collection.mutable._

-------------------------------------------------------------------------------

scala> a += 1
res1: a.type = ArrayBuffer(1)

scala> a += 2
res2: a.type = ArrayBuffer(1, 2)

scala> a += (10,20,40)
res3: a.type = ArrayBuffer(1, 2, 10, 20, 40)

//往变长数组里面添加值单个值和多个值

--------------------------------------------------------------------------------------

scala> a.
++ combinations groupBy mapResult reverse to
++: companion grouped max reverseIterator toArray
++= compose hasDefiniteSize maxBy reverseMap toBuffer
++=: contains hashCode min runWith toIndexedSeq
+: containsSlice head minBy sameElements toIterable
+= copyToArray headOption mkString scan toIterator
+=: copyToBuffer indexOf nonEmpty scanLeft toList
- corresponds indexOfSlice orElse scanRight toMap
-- count indexWhere padTo segmentLength toSeq
--= diff indices par seq toSet
-= distinct init partition size toStream
/: drop inits patch sizeHint toString
:+ dropRight insert permutations sizeHintBounded toTraversable
:\ dropWhile insertAll prefixLength slice toVector
<< endsWith intersect prepend sliding transform
WithFilter equals isDefinedAt prependAll sortBy transpose
addString exists isEmpty product sortWith trimEnd
aggregate filter isTraversableAgain readOnly sorted trimStart
andThen filterNot iterator reduce span union
append find last reduceLeft splitAt unzip
appendAll flatMap lastIndexOf reduceLeftOption startsWith unzip3
apply flatten lastIndexOfSlice reduceOption stringPrefix update
applyOrElse fold lastIndexWhere reduceRight sum updated
canEqual foldLeft lastOption reduceRightOption tail view
clear foldRight length reduceToSize tails withFilter
clone forall lengthCompare remove take zip
collect foreach lift repr takeRight zipAll
collectFirst genericBuilder map result takeWhile zipWithIndex

//ArrayBuffer的方法有很多

------------------------------------------------------------------------------------------

res5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 10, 20, 40)

scala> a.tr
transform transpose trimEnd trimStart

scala> a.trimEnd(1)

scala> a
res7: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 10, 20)

//trimEnd方法是删除数组后面的元素

==========================================================

3、遍历数组

scala> val a = Array(10,30,40)
a: Array[Int] = Array(10, 30, 40)

scala>

scala> for(s <- a) println(s)
10
30
40

scala> a.foreach(println)
10
30
40

==========================================================

4、数组的常见操作

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

scala> a.max
res11: Int = 9

//取数组的最大值

--------------------------------------------------------------

scala> a.min
res12: Int = 1

//取数组的最小值

----------------------------------------------------------

scala> a.sort
sortBy sortWith sorted

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

//将数组按从大到小排序

---------------------------------------------------------------

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

//将数组按从小到大排序

//a.sortWith(_<_)的完整写法是a.sortWith((a,b) => { if (a>b) ture else fase })

//这个函数是匿名函数没有def关键字和函数名只有参数列表和=>

==================================================

5、多维数组

5.1、和Java一样,数组套数组,定义一个固定长度的二维数组

scala> val matrix = Array.ofDim[Int](3,4)
matrix: Array[Array[Int]] = Array(Array(0, 0, 0, 0), Array(0, 0, 0, 0), Array(0, 0, 0, 0))

--------------------------------------------------------------------------------------------------

scala> matrix(2)(3) = 10

scala> matrix
res16: Array[Array[Int]] = Array(Array(0, 0, 0, 0), Array(0, 0, 0, 0), Array(0, 0, 0, 10))

//给二维数组的第三个Array里面的第四个元素赋值10,和Java一样数组的下标从0开始的

------------------------------------------------------------------------------------------------------------

5.2、定义一个非固定长度的二维数组

scala> val a = new Array[Array[Int]](5)
a: Array[Array[Int]] = Array(null, null, null, null, null)

scala> for(s <- 0 until a.length)
| a(s) = new Array(s+1)

scala> a
res18: Array[Array[Int]] = Array(Array(0), Array(0, 0), Array(0, 0, 0), Array(0, 0, 0, 0), Array(0, 0, 0, 0, 0))

//非固定长度的二维数组定义

 

 

Scala--数组类型

标签:reducer   exist   filter   rem   pen   rev   clone   element   dex   

原文地址:https://www.cnblogs.com/jeff190812/p/11822942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!