原创文章,转载请注明出处!

原文地址: http://www.ptbird.cn/2016/07/19/scala-chuji-demo-03/

There I am,in the world more exciting!

by postbird

www.ptbird.cn


03数组的操作


 
package com.ptbird.scala import scala.collection.mutable.ArrayBuffer /** * Created by postbird on 2016/5/24. */ object ArrayTest { def main(args: Array[String]): Unit = { val a = new Array[Int](10) val b = new Array[String](10) val c = Array("AAAA","bb") //println(c(1)) //for( elem <- c) println(elem) //for( i <- 0 until c.length) println(c(i)) val d= ArrayBuffer[String]() //增加值 d+="Hell" //在某个位置插入值 d.insert(1,"aaa","ccc","ccc","ccc","ccc") //移除值 从2开始移除2个 d.remove(2,1) //println(d) //数组的遍历 for(i<-1 until d.length){ //println(d(i)) } //使用yield保存数据 val e =Array(2,3,4,5) val result = for(i<-e) yield i+1 //println(result(1)) //通过filter循环bianli e.filter(_ % 2==0).map(2 *_) //计算 println(e.sum) //排序 e.sorted for(i <- e) println("--"+i) //调用包里面的 scala.util.Sorting.quickSort(e) for(i <- e) println("-"+i) //max println(e.max) //字符串的拼接 // val s:String = e.mkString(" + ")//2 + 3 + 4 + 5 val s:String = e.mkString("<",",",">") //<2,3,4,5> println(s) } }