One-Way
爱一人,攀一山,追一梦

一、定长数组

Array定长数组,访问数组元素需要通过()  数组长度是固定的,但是内容可以修改

val nums = new Array[Int](10)  //长度为10的int数组 初始化为0
val strs = new Array[String](10) //长度为10的String数组 初始化为null
val s = Array("Hello", "World") //初始化数组长度为2,不需要new
s(0) = "GoodBye"  //访问数组元素通过()
println(nums(0))
println(strs(0))
println(s(0)+" "+s(1))

结果

0
null
GoodBye World

 

二、变长数组

变长数组使用ArrayBuffer

import scala.collection.mutable.ArrayBuffer

val b = ArrayBuffer[Int]() //或者 new ArrayBuffer[Int]

b+=1
  //在尾端添加元素
  //ArrayBuffer(1)
b+=(2,3,4,5)
  //在尾端添加多个元素
  //ArrayBuffer(1,2,3,4,5)
b++=ArrayBuffer(8,19,20)
  //可以使用++-操作符追加任意集合
  //ArrayBuffer(1,2,3,4,5,8,19,20)
b.trimEnd(3)
  //在尾部删除3个元素
  //ArrayBuffer(1,2,3,4,5)
b.insert(2,6)
  //在下标2之前插入6
  //ArrayBuffer(1,2,6,3,4,5)
b.insert(3,7,8,9)
  //在下标3之前插入7,8,9
  //ArrayBuffer(1,2,6,7,8,9,3,4,5)
b.remove(2)
  //删除下标为2的元素
  //ArrayBuffer(1,2,7,8,9,3,4,5)
b.remove(2,3)
  //从下标为2的元素开始,删除3个元素
  //ArrayBuffer(1,2,3,4,5)

val c = b.toArray //不知道元素个数先构建ArrayBuffer,然后再转化成数组
println(c(0))
println(c.toBuffer) //数组转化成数组缓冲

结果

1
ArrayBuffer(1, 2, 3, 4, 5)

 

三、遍历数组和数组缓冲

for(i<- 0 until b.length) print(c(i)) //直接通过下标访问数组,until 不包括上线
println()
for(i<- 0 until (b.length,2))  print(b(i)) //step = 2 步长为2
println()
for(i<- (0 until b.length).reverse)  print(b(i)) //反转
println()
for(i<- b) print(i) //不使用数组下标直接访问元素
println()

结果

 12345
  135
  54321
  12345

 

四、数组转换

通过for yield对数据进行转换

val a = Array(2,3,5,7,11)
val b = for(i<- a if i%2 !=0)  yield i * 2 //生成一个新的数组
for(i<- b) print(i+" ")

结果

6 10 14 22

 

五、常用算法

import scala.util.Sorting.quickSort
val a = Array(2,9,5,7,11)

println(a.sum)
println(a.max)
println(a.toBuffer)
println(a.mkString(" and "))
println(a.mkString("<"," and ",">"))
println(a.toBuffer.sorted)         //数组本身不变,产生新的数组,升序排列
println(a.toBuffer.sortWith(_>_))  //数组本身不变,产生新的数组,降序排列
println(a.toString)
println(a.toBuffer.toString())
quickSort(a)  //对数组本身排序

结果

34
11
ArrayBuffer(2, 9, 5, 7, 11)
2 and 9 and 5 and 7 and 11
<2 and 9 and 5 and 7 and 11>
ArrayBuffer(2, 5, 7, 9, 11)
ArrayBuffer(11, 9, 7, 5, 2)
[I@4a3d0611
ArrayBuffer(2, 9, 5, 7, 11)

 

六、解读scaladoc

 

七、多维数组

 

八、与java互操作

 

 

1.

    val r = scala.util.Random
    val f = (n:Int)=>{
      val arr = new Array[Int](n)
      for(i<- 0 until n) arr(i)=r.nextInt(n)
      arr
    }
    val a =f(10)
    println(a.toBuffer.toString())

2.

    val arr = Array(1,2,3,4,5)

    for(i<- 0 until arr.length/2 ){
      val tmp = arr(i*2+1)
      arr(i*2+1)=arr(i*2)
      arr(i*2)=tmp
    }

    println(arr.toBuffer.toString())

3.

    val new_arr = for(i<- 0 until arr.length)
      yield
      if(i%2==0){
        if(i==arr.length-1)
          arr(i)
        else
          arr(i+1)
      }else{
        arr(i-1)
      }
    println(new_arr.toBuffer.toString())

4.

    val a = Array(3,0,4,7,9,-8,-2,2,1,0,5)
    val new_a = for(j<- 1 to 2 ;i<- 0 until a.length if j==1 && a(i)>0 || j==2 && a(i)<=0 )  yield  a(i)

    println(new_a.toBuffer.toString())

5.

    val d = Array[Double](1,2,5,9,3,2)

    println(d.sum/d.length)

6.

    val d = Array(1,2,5,9,3,2)
    println(d.sorted.toBuffer.toString)
    println(d.sortWith(_>_).toBuffer.toString())
    scala.util.Sorting.quickSort(d)
    println(d.toBuffer.toString())

    val db = ArrayBuffer (1,2,5,9,3,2)
    println(db.sorted)
    println(db.sortWith(_>_))

7.

    val db = ArrayBuffer (1,2,5,9,3,2)

    println(db.distinct.toString())

 

 

 

 

 

 

 

参考《快学Scala》

posted on 2016-08-23 01:08  单行道|  阅读(9501)  评论(0编辑  收藏  举报