zoukankan      html  css  js  c++  java
  • 【scala】数组和列表

    一、数组Array

    1.创建数组

      隐式:val greetStrings = new Array[String](3);

      显式:val greetStrings : Array[String] = new Arrray[String](3);

    2.赋值

      (1)greetStrings(0) = "hello";

          greetStrings(1) = ",";

          greetStrings(2) = "world ";   //注意,后边是括号(),并不是[ ]

      (2)val greetStrings = Array("hello",",","world ");//编译器会推断出数组的类型为Array[String],同样后边是()并不是[ ]

      (3)greetStings.update(0,"Hello");//改变0位置的元素

    3.遍历

      for(i <- 0 until greetStrings.length)

        println(i + ":" + greetingString(i));

    4.多维数组

    var myMatrix = ofDim[Int](3,3) //创建一个3x3的Int类型的多维数组
    for (i <- 0 to 2;j <- 0 to 2) {
                myMatrix(i)(j) = 1; //可以通过matrix(i)(j)来访问数组
                print(myMatrix(i)(j));
     }      

    5.一些常用方法

    1

    def apply( x: T, xs: T* ): Array[T]

    创建指定对象 T 的数组, T 的值可以是 Unit, Double, Float, Long, Int, Char, Short, Byte, Boolean。

    2

    def concat[T]( xss: Array[T]* ): Array[T]

    合并数组

    3

    def copy( src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int ): Unit

    复制一个数组到另一个数组上。相等于 Java's System.arraycopy(src, srcPos, dest, destPos, length)。

    4

    def empty[T]: Array[T]

    返回长度为 0 的数组

    5

    def iterate[T]( start: T, len: Int )( f: (T) => T ): Array[T]

    返回指定长度数组,每个数组元素为指定函数的返回值。

    以上实例数组初始值为 0,长度为 3,计算函数为a=>a+1

    scala> Array.iterate(0,3)(a=>a+1)
    res1: Array[Int] = Array(0, 1, 2)
    6

    def fill[T]( n: Int )(elem: => T): Array[T]

    返回数组,长度为第一个参数指定,同时每个元素使用第二个参数进行填充。

    7

    def fill[T]( n1: Int, n2: Int )( elem: => T ): Array[Array[T]]

    返回二数组,长度为第一个参数指定,同时每个元素使用第二个参数进行填充。

    8

    def ofDim[T]( n1: Int ): Array[T]

    创建指定长度的数组

    9

    def ofDim[T]( n1: Int, n2: Int ): Array[Array[T]]

    创建二维数组

    10

    def ofDim[T]( n1: Int, n2: Int, n3: Int ): Array[Array[Array[T]]]

    创建三维数组

    11

    def range( start: Int, end: Int, step: Int ): Array[Int]

    创建指定区间内的数组,step 为每个元素间的步长

    12

    def range( start: Int, end: Int ): Array[Int]

    创建指定区间内的数组

    13

    def tabulate[T]( n: Int )(f: (Int)=> T): Array[T]

    返回指定长度数组,每个数组元素为指定函数的返回值,默认从 0 开始。

    以上实例返回 3 个元素:

    scala> Array.tabulate(3)(a => a + 5)
    res0: Array[Int] = Array(5, 6, 7)
    14

    def tabulate[T]( n1: Int, n2: Int )( f: (Int, Int ) => T): Array[Array[T]]

    返回指定长度的二维数组,每个数组元素为指定函数的返回值,默认从 0 开始。

    二、列表list

    1.创建列表

      val oneTwoThree = List(1,2,3);

      val oneTwoThree:List[Int] = List(1,2,3); //注意,因为list不可变不能向数组那样,先实例再赋值,必须实例的时候就进行赋值。

    2.遍历

      (1)for(i <- 0 until oneTwoThree.length)

        println(i + ":" + oneTwoThree(i));//同数组,随机访问是list(i),不是list[i]

      (2)println(oneTwoThree); //注意数组不能这么直接打印

         (3)oneTwoThree.foreach(print)

    3.列表拼接  

     (1)List有个办法叫“:::”用于两个列表拼接。

      val one = List(1,2);

      val two = List(3,4);

      var oneAddTwo = one ::: two;

     (2)在已有列表前添加一个元素 “::”

      val three = 5 :: one ;

      val one = 1 :: 2 :: Nil; //创建一个one = List(1,2);Nil代表一个空List。

      有同学要问,为什么不在list表尾添加元素呢,非要在表头添加呢?其实List.append(或 + )方法提供了在list表尾添加元素的功能。

      但是在结尾添加元素的操作时间随着列表的大小线性增加,而使用::在表头添加的话只需要常量时间。

      如果非要在表尾添加元素,我们可以通过依次在表头添加,然后调用list.reverse方法反序列表,达成高效建表。

     (3)一些常用方法

      

    1

    def +:(elem: A): List[A]

    为列表预添加元素

    scala> val x = List(1)
    x: List[Int] = List(1)
    
    scala> val y = 2 +: x
    y: List[Int] = List(2, 1)
    
    scala> println(x)
    List(1)
    2

    def ::(x: A): List[A]

    在列表开头添加元素

    3

    def :::(prefix: List[A]): List[A]

    在列表开头添加指定列表的元素

    4

    def :+(elem: A): List[A]

    复制添加元素后列表。

    scala> val a = List(1)
    a: List[Int] = List(1)
    
    scala> val b = a :+ 2
    b: List[Int] = List(1, 2)
    
    scala> println(a)
    List(1)
    5

    def addString(b: StringBuilder): StringBuilder

    将列表的所有元素添加到 StringBuilder

    6

    def addString(b: StringBuilder, sep: String): StringBuilder

    将列表的所有元素添加到 StringBuilder,并指定分隔符

    7

    def apply(n: Int): A

    通过列表索引获取元素

    8

    def contains(elem: Any): Boolean

    检测列表中是否包含指定的元素

    9

    def copyToArray(xs: Array[A], start: Int, len: Int): Unit

    将列表的元素复制到数组中。

    10

    def distinct: List[A]

    去除列表的重复元素,并返回新列表

    11

    def drop(n: Int): List[A]

    丢弃前n个元素,并返回新列表

    12

    def dropRight(n: Int): List[A]

    丢弃最后n个元素,并返回新列表

    13

    def dropWhile(p: (A) => Boolean): List[A]

    从左向右丢弃元素,直到条件p不成立

    14

    def endsWith[B](that: Seq[B]): Boolean

    检测列表是否以指定序列结尾

    15

    def equals(that: Any): Boolean

    判断是否相等

    16

    def exists(p: (A) => Boolean): Boolean

    判断列表中指定条件的元素是否存在。

    判断l是否存在某个元素:

    scala> l.exists(s => s == "Hah")
    res7: Boolean = true
    17

    def filter(p: (A) => Boolean): List[A]

    输出符号指定条件的所有元素。

    过滤出长度为3的元素:

    scala> l.filter(s => s.length == 3)
    res8: List[String] = List(Hah, WOW)
    18

    def forall(p: (A) => Boolean): Boolean

    检测所有元素。

    例如:判断所有元素是否以"H"开头:

    scala> l.forall(s => s.startsWith("H")) res10: Boolean = false
    19

    def foreach(f: (A) => Unit): Unit

    将函数应用到列表的所有元素

    20

    def head: A

    获取列表的第一个元素

    21

    def indexOf(elem: A, from: Int): Int

    从指定位置 from 开始查找元素第一次出现的位置

    22

    def init: List[A]

    返回所有元素,除了最后一个

    23

    def intersect(that: Seq[A]): List[A]

    计算多个集合的交集

    24

    def isEmpty: Boolean

    检测列表是否为空

    25

    def iterator: Iterator[A]

    创建一个新的迭代器来迭代元素

    26

    def last: A

    返回最后一个元素

    27

    def lastIndexOf(elem: A, end: Int): Int

    在指定的位置 end 开始查找元素最后出现的位置

    28

    def length: Int

    返回列表长度

    29

    def map[B](f: (A) => B): List[B]

    通过给定的方法将所有元素重新计算

    30

    def max: A

    查找最大元素

    31

    def min: A

    查找最小元素

    32

    def mkString: String

    列表所有元素作为字符串显示

    33

    def mkString(sep: String): String

    使用分隔符将列表所有元素作为字符串显示

    34

    def reverse: List[A]

    列表反转

    35

    def sorted[B >: A]: List[A]

    列表排序

    36

    def startsWith[B](that: Seq[B], offset: Int): Boolean

    检测列表在指定位置是否包含指定序列

    37

    def sum: A

    计算集合元素之和

    38

    def tail: List[A]

    返回所有元素,除了第一个

    39

    def take(n: Int): List[A]

    提取列表的前n个元素

    40

    def takeRight(n: Int): List[A]

    提取列表的后n个元素

    41

    def toArray: Array[A]

    列表转换为数组

    42

    def toBuffer[B >: A]: Buffer[B]

    返回缓冲区,包含了列表的所有元素

    43

    def toMap[T, U]: Map[T, U]

    List 转换为 Map

    44

    def toSeq: Seq[A]

    List 转换为 Seq

    45

    def toSet[B >: A]: Set[B]

    List 转换为 Set

    46

    def toString(): String

    列表转换为字符串

    三、数组和列表的联系与区别

      Scala数组是一个拥有相同类型的对象的可变序列,也就是说一个数组中只能包含相同类型的元素。虽然无法在数组实例化以后改变其长度,但是却可以改变它的元素值,因此,数组是可变的对象

      Scala列表,跟数组类似只能包含相同类型的元素,但是列表是不可变的,每次改变List的操作都会返回一个新的List,这有点类似于JAVA里的String呀有木有。

      注意,Scala中的List和JAVA中的List虽然拥有相同的名字,但是可不是一个东西,区别很大。

  • 相关阅读:
    [APIO2014]序列分割
    [HNOI2014]世界树
    [THUWC2017]随机二分图
    快乐游戏鸡
    [SHOI2014]三叉神经树
    带花树学习笔记
    最小树形图——朱刘算法学习笔记
    【WC2018】即时战略
    [HNOI2015]接水果
    [HAOI2018]染色
  • 原文地址:https://www.cnblogs.com/zzhangyuhang/p/8663071.html
Copyright © 2011-2022 走看看