赞
踩
scala中集合默认是不可变的,在包scala.collection.immutable下,
任何对集合的修改,添加,删除操作都是生成一个新的集合返回
如果想对集合进行修改,添加,删除操作,需要使用scala.collection.mutable
包下得集合Buffer类
scala> var list=List(1,2,3) list: List[Int] = List(1, 2, 3) scala> list(1)=10 <console>:17: error: value update is not a member of List[Int] list(1)=10 ^ scala> import scala.collection.mutable._ import scala.collection.mutable._ scala> var list2=ListBuffer(1,2,3) list2: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3) scala> list2(1)=10 scala> list2 res13: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 10, 3)
List:元素以线性方式存储,集合中可以存放重复对象
scala> var a = List(1,2,3)
a: List[Int] = List(1, 2, 3)
scala> var a = List[Int](1,2,3)
a: List[Int] = List(1, 2, 3)
scala> var a = List[String]("a","b","c")
a: List[String] = List(a, b, c)
scala> var a:List[Int]=List(1,2,3)
a: List[Int] = List(1, 2, 3)
+:/:+ 添加元素,List接在:一边 +: scala> a res26: List[Int] = List(1, 2, 3) scala> 0+:a res27: List[Int] = List(0, 1, 2, 3) :+ scala> a res26: List[Int] = List(1, 2, 3) scala> a:+4 res28: List[Int] = List(1, 2, 3, 4) :: 添加元素(元素在左,List在右)添加在List开头 def ::(x: A): List[A] scala> a::6 <console>:20: error: value :: is not a member of Int a::6 ^ scala> 6::a res30: List[Int] = List(6, 1, 2, 3)
def addString(b: StringBuilder): StringBuilder def addString(b: StringBuilder, sep: String): StringBuilder scala> a res36: List[String] = List(a, b, c) scala> var b = new StringBuilder b: StringBuilder = scala> a.addString(b) res37: StringBuilder = abc //不初始化就会在后面追加 scala> var b = new StringBuilder b: StringBuilder = scala> a.addString(b,",") res38: StringBuilder = a,b,c
def :::(prefix: List[A]): List[A]
scala> a
res39: List[String] = List(a, b, c)
scala> var b = List(1,2,3)
b: List[Int] = List(1, 2, 3)
scala> a:::b
res40: List[Any] = List(a, b, c, 1, 2, 3)
def apply(n: Int): A
scala> a
res41: List[String] = List(a, b, c)
scala> a.apply(1)
res42: String = b
def contains(elem: Any): Boolean
scala> a
res43: List[String] = List(a, b, c)
scala> a.contains("a")
res44: Boolean = true
scala> a.contains("e")
res45: Boolean = false
def copyToArray(xs: Array[A], start: Int, len: Int): Unit scala> var b=new Array[String](3) b: Array[String] = Array(null, null, null) scala> a res46: List[String] = List(a, b, c) scala> b res47: Array[String] = Array(null, null, null) scala> a.copyToArray(b) scala> b res49: Array[String] = Array(a, b, c) 方法后面可以跟数组的下标起始位置(左闭右开) scala> a res59: List[String] = List(a, b, c, d, e) scala> var b=new Array[String](3) b: Array[String] = Array(null, null, null) scala> a.copyToArray(b,0,3) scala> b res61: Array[String] = Array(a, b, c)
def distinct: List[A]
scala> var a=List(1,1,2,2,3,3)
a: List[Int] = List(1, 1, 2, 2, 3, 3)
scala> a.distinct
res62: List[Int] = List(1, 2, 3)
def drop(n: Int): List[A]
scala> var a=List(1,1,2,2,3,3)
a: List[Int] = List(1, 1, 2, 2, 3, 3)
scala> a.drop(2)
res69: List[Int] = List(2, 2, 3, 3)
def dropRight(n: Int): List[A]
scala> a
res70: List[Int] = List(1, 1, 2, 2, 3, 3)
scala> a.dropRight(2)
res71: List[Int] = List(1, 1, 2, 2)
def dropWhile(p: (A) => Boolean): List[A]
scala> a
res89: List[Int] = List(1, 1, 2, 2, 3, 3)
scala> a.dropWhile(_==1)
res91: List[Int] = List(2, 2, 3, 3)
scala> a.dropWhile(_>2)
res92: List[Int] = List(1, 1, 2, 2, 3, 3)
def equals(that: Any): Boolean
scala> a
res100: List[String] = List(abc)
scala> b
res101: Array[String] = Array(a, b, c)
scala> a.equals(b)
res102: Boolean = false
def exists(p: (A) => Boolean): Boolean
scala> a
res111: List[Int] = List(1, 2, 3)
scala> a.exists(x=>x==2)
res112: Boolean = true
def filter(p: (A) => Boolean): List[A]
scala> var a=List("aaa","aa","a","bbb")
a: List[String] = List(aaa, aa, a, bbb)
scala> a.filter(x=>x.size==3)
res113: List[String] = List(aaa, bbb)
def forall(p: (A) => Boolean): Boolean
scala> var a=List("aaa","aa","a")
a: List[String] = List(aaa, aa, a)
scala> a.forall(a=>a.startsWith("a"))
res115: Boolean = true
def foreach(f: (A) => Unit): Unit
scala> a
res134: List[String] = List(ccc, bb, b, bbb)
scala> a.foreach(println)
ccc
bb
b
bbb
def head: A
scala> a
res14: List[Int] = List(1, 2, 3)
scala> a.head
res15: Int = 1
def indexOf(elem: A, from: Int): Int
scala> var a=List(1,2,3)
a: List[Int] = List(1, 2, 3)
scala> a.indexOf(2)
res141: Int = 1
def init: List[A]
scala> a
res144: List[Int] = List(1, 2, 3)
scala> a.init
res145: List[Int] = List(1, 2)
def tail: List[A]
scala> a
res144: List[Int] = List(1, 2, 3)
scala> a.tail
res17: List[Int] = List(2, 3)
def intersect(that: Seq[A]): List[A]
scala> a
res146: List[Int] = List(1, 2, 3)
scala> var b = List(3,4,5)
b: List[Int] = List(3, 4, 5)
scala> a.intersect(b)
res147: List[Int] = List(3)
def isEmpty: Boolean
scala> a
res146: List[Int] = List(1, 2, 3)
scala> a.isEmpty
res18: Boolean = false
def iterator: Iterator[A] scala> a res148: List[Int] = List(1, 2, 3) scala> a.iterator res149: Iterator[Int] = non-empty iterator scala> var b=a.iterator b: Iterator[Int] = non-empty iterator scala> println(b.next()) 1 scala> println(b.next()) 2 scala> println(b.next()) 3
def last: A
scala> a
res153: List[Int] = List(1, 2, 3)
scala> a.last
res155: Int = 3
def lastIndexOf(elem: A, end: Int): Int
scala> var a=List(1,2,3,1,3,4,1,5,1)
a: List[Int] = List(1, 2, 3, 1, 3, 4, 1, 5, 1)
scala> a.lastIndexOf(1)
res157: Int = 8
scala> a.lastIndexOf(1,5)
res158: Int = 3
def length: Int
scala> a
res159: List[Int] = List(1, 2, 3, 1, 3, 4, 1, 5, 1)
scala> a.length
res160: Int = 9
scala> a.size
res161: Int = 9
def map[B](f: (A) => B): List[B]
scala> a
res162: List[Int] = List(1, 2, 3, 1, 3, 4, 1, 5, 1)
scala> a.map(x=>x+2)
res163: List[Int] = List(3, 4, 5, 3, 5, 6, 3, 7, 3)
def max/min: A
scala> a
res165: List[Int] = List(1, 2, 3, 1, 3, 4, 1, 5, 1)
scala> a.max
res166: Int = 5
scala> a.min
res167: Int = 1
def mkString: String
def mkString(sep: String): String
scala> a
res168: List[Int] = List(1, 2, 3, 1, 3, 4, 1, 5, 1)
scala> a.mkString
res169: String = 123134151
scala> a.mkString(" ")
res170: String = 1 2 3 1 3 4 1 5 1
def reverse: List[A]
scala> var a = List(1,2,3,4)
a: List[Int] = List(1, 2, 3, 4)
scala> a.reverse
res171: List[Int] = List(4, 3, 2, 1)
def sorted[B >: A]: List[A]
scala> var a=List(1,2,3,1,2,3,5,4,2,7,2)
a: List[Int] = List(1, 2, 3, 1, 2, 3, 5, 4, 2, 7, 2)
scala> a.sorted
res172: List[Int] = List(1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 7)
def sum: A
scala> a
res174: List[Int] = List(1, 2, 3, 1, 2, 3, 5, 4, 2, 7, 2)
scala> a.sum
res175: Int = 32
def take(n: Int): List[A]
scala> a
res176: List[Int] = List(1, 2, 3, 1, 2, 3, 5, 4, 2, 7, 2)
scala> a.take(5)
res177: List[Int] = List(1, 2, 3, 1, 2)
def takeRight(n: Int): List[A]
scala> a
res176: List[Int] = List(1, 2, 3, 1, 2, 3, 5, 4, 2, 7, 2)
scala> a.takeRight(5)
res178: List[Int] = List(5, 4, 2, 7, 2)
def toArray: Array[A]
scala> a
res179: List[Int] = List(1, 2, 3, 1, 2, 3, 5, 4, 2, 7, 2)
scala> a.toArray
res180: Array[Int] = Array(1, 2, 3, 1, 2, 3, 5, 4, 2, 7, 2)
def toBuffer[B >: A]: Buffer[B]
scala> a.toBuffer
res181: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3, 1, 2, 3, 5, 4, 2, 7, 2)
def toMap[T, U]: Map[T, U]
scala> var a = List("cat","bird","tiger")
a: List[String] = List(cat, bird, tiger)
scala> a.map(a=>(a.size,a))
res184: List[(Int, String)] = List((3,cat), (4,bird), (5,tiger))
scala> a.map(a=>(a.size,a)).toMap
res185: scala.collection.immutable.Map[Int,String] = Map(3 -> cat, 4 -> bird, 5 -> tiger)
def toSet[B >: A]: Set[B]
scala> var a = List("cat","bird","tiger")
a: List[String] = List(cat, bird, tiger)
scala> a.map(a=>(a.size,a))
res184: List[(Int, String)] = List((3,cat), (4,bird), (5,tiger))
scala> a.map(a=>(a.size,a)).toSet
res186: scala.collection.immutable.Set[(Int, String)] = Set((3,cat), (4,bird), (5,tiger))
def toString(): String
scala> var a=List(1,2,3)
a: List[Int] = List(1, 2, 3)
scala> a.toString
res187: String = List(1, 2, 3)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。