Scala 集合 List操作
package scala_learn.demo11_Collection
object O2_List {
def main(args: Array[String]) {
test07()
}
def test4(): Unit = {
val ls = List(5, 7, 9, 12, 8, 2, 76)
ls match {
case l: List[Int] => println("right list")
case _ => println("error")
}
}
def test3(): Unit = {
val ls = List(5, 7, 9, 12, 8, 2, 76)
val it = ls.iterator
while (it.hasNext) {
print(it.next() + "\t")
}
}
def test2(): Unit = {
val ls = List(5, 7, 9, 12, 8, 2, 76)
for (i <- 0 until (ls.size)) {
print(ls(i) + "\t")
}
}
def test1(): Unit = {
val ls = List(5)
println(ls.size)
println(ls(0))
}
def test00(): Unit = {
val fruit: List[String] = List("apples", "oranges", "pears")
val nums: List[Int] = List(1, 2, 3, 4)
val empty: List[Nothing] = List()
val dim: List[List[Int]] = List(List(1, 0, 0), List(0, 1, 0), List(0, 0, 1))
}
def tes01(): Unit = {
val fruit = "apples" :: ("oranges" :: ("pears" :: Nil))
val nums = 1 :: (2 :: (3 :: (4 :: Nil)))
val empty = Nil
val dim = (1 :: (0 :: (0 :: Nil))) :: (0 :: (1 :: (0 :: Nil))) :: (0 :: (0 :: (1 :: Nil))) :: Nil
}
def test02(): Unit = {
val fruit: List[String] = List("apples", "oranges", "pears")
println(fruit.head)
println(fruit.tail)
println(fruit.isEmpty)
}
def test03(): Unit = {
val fruit: List[String] = List("apples", "oranges", "pears")
val people: List[String] = List("zhangsan", "lisi", "wangwu")
val nickNames: List[String] = List("qingfengxia", "zhizhuxia", "gangtiexia")
val l1 = people.:::(nickNames)
println("l1=" + l1)
val l2 = people ::: nickNames ::: fruit
println("l2=" + l2)
val l3 = List(people, nickNames, fruit)
println("l3=" + l3)
println(people)
println(nickNames)
println(fruit)
}
def test04(): Unit = {
val l1 = List(5, 7, 4, 2, 6, 9)
println("list=" + l1 + " head:" + l1.head + " tail:" + l1.tail)
println(l1.tail.head)
println("list=" + l1 + " init:" + l1.init + " tail:" + l1.last)
println(l1.init.last)
}
def test05(): Unit = {
val l1 = List(5, 7, 4, 2, 6, 9)
println(l1.take(2))
println(l1.takeWhile(_ > 4))
println(l1.drop(2))
println(l1.dropRight(2))
println("?? " + l1.dropWhile(_ > 4))
println(l1.apply(0))
println(l1(0))
println(l1.indices)
println(l1.partition(_ % 2 == 0))
println(l1.span(_ > 4))
println(l1.find(_ % 2 == 0))
println(l1.exists(_ % 2 == 0))
println(l1.forall(_ % 2 == 0))
println(l1.sortWith(_ > _))
println(l1)
println(l1.sortWith(_ < _))
println(l1)
}
def test06(): Unit = {
var l1 = List(3, 2, 4, 5)
println(l1)
l1 = List.apply(3, 2, 4, 5)
println(l1)
l1 = List.make(4, 23)
println(l1)
l1 = List.range(1, 5)
println(l1)
l1 = List.range(1, 5, 2)
println(l1)
}
def test07(): Unit = {
val s1 = List("zhang", "li", "zhao", "huang")
val a1 = List(13, 12, 14, 15)
val r1 = s1.zip(a1)
println(r1)
val r2 = a1.zip(s1)
println(r2)
println(r1.unzip)
println(r2.unzip)
println(s1.zipWithIndex)
println(List(List(3, 5), List("a", "b"), List("h", "k")).flatten)
println(List.concat(List(3, 5), List("a", "b"), List("h", "k")))
val l3 = List(3, 2, 4, 5)
println(l3.map(_ * 2))
val l4 = List(List(3, 2), List(4, 5))
println(l4.flatMap(_.map(_ * 2)))
val r3 =List.map2(List(3, 2),List(3, 2))(_+_)
println(r3)
}
}