一、数组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\n";   //注意,后边是括号(),并不是[ ]

  (2)val greetStrings = Array("hello",",","world\n");//编译器会推断出数组的类型为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虽然拥有相同的名字,但是可不是一个东西,区别很大。

【scala】数组和列表的更多相关文章

  1. Spark记录-Scala数组

    Scala提供了一种数据结构叫作数组,数组是一种存储了相同类型元素的固定大小顺序集合.数组用于存储数据集合,但将数组视为相同类型变量的集合通常更为有用. 可以声明一个数组变量,例如:numbers,使 ...

  2. Scala数组和集合

    一.scala数组 数组定义1: var arr = new Array[String](3) String:存储的元素类型 3:存储3个元素 添加元素: arr(1) = "hello&q ...

  3. scala数组

    #scala数组 val A= new Array[T](N) val A = new Array[Int](10) ##变长数组 import scala.collection.mutable.Ar ...

  4. [改善Java代码]避开基本类型数组转换列表陷阱

    开发中经常用到Arrays和Collections这两个工具类. 在数组和列表之间进行切换.非常方便.但是也会遇到一些问题. 看代码: import java.util.Arrays; import ...

  5. Scala数组| 集合

    arrays :+ 5尾部   头部5 +: arrays TODO 声明不可变数组,不能删; 默认情况下,scala中集合的声明全都是不可变的 val arrays: Array[Int] = Ar ...

  6. 【scala】scala 数组 (三)

    基础内容 1. 数组定义 定长.可变数组的定义;元素添加,删除,排序,求和等常用运算 import scala.collection.mutable.ArrayBuffer import scala. ...

  7. 3、scala数组

    一.Array .Array Buffer 1.Array 在Scala中,Array代表的含义与Java中类似,也是长度不可改变的数组. 此外,由于Scala与Java都是运行在JVM中,双方可以互 ...

  8. 4、scala数组

    1.Array 2.ArrayBuffer 3.遍历Array和ArrayBuffer 4.数组常见操作 1.  Array Scala中,array代表的含义与java类似,也是长度不可改变的数组. ...

  9. Scala 数组和List

    Scala 数组和List: import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.Buffer ob ...

随机推荐

  1. oracle Dba之路

    如何快速的成为一个合格的 DBA? 2010年11月03日 11:25:00 阅读数:584 原文来自:http://topic.csdn.net/u/20101031/21/A78B2EA1-6F2 ...

  2. 005-shiro认证

    一.shiro认证流程 二.入门程序 1.代码: 2.配置shiro-first.ini 通过此配置文件创建securityManager工厂. 需要修改eclipse的ini的编辑器: 配置数据: ...

  3. 解读tensorflow之rnn 的示例 ptb_word_lm.py

    这两天想搞清楚用tensorflow来实现rnn/lstm如何做,但是google了半天,发现tf在rnn方面的实现代码或者教程都太少了,仅有的几个教程讲的又过于简单.没办法,只能亲自动手一步步研究官 ...

  4. SQL基础二

    一.SQL SELECT 语句 SELECT 语句用于从表中选取数据.结果被存储在一个结果表中(称为结果集). SQL SELECT 语法: SELECT 列名称 FROM 表名称 以及: SELEC ...

  5. Deep learning Reading List

    本文来自:http://jmozah.github.io/links/ Following is a growing list of some of the materials i found on ...

  6. sersync的confxml.xml文件详解

    <?xml version="1.0" encoding="ISO-8859-1"?> <head version="2.5&quo ...

  7. 236. Lowest Common Ancestor of a Binary Tree(最低公共祖先,难理解)

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  8. ubuntu中在Launcher上添加Android Studio的运行图标

    运行命令创建desktop文件: sudo gedit /usr/share/applications/android_studio.desktop 打开窗口后输入以下内容,注意Exec和Icon要修 ...

  9. Nginx rewrite配置

    rewrite应用 Rewrite模块设置及Wordpress和Discuz的示例.Nginx的Rewrite规则比Apache的简单灵活多了,从下面介绍可见一斑. rewrite配置 Nginx可以 ...

  10. git 强制放弃本地修改(新增、删除文件)【转】

    本文转载自:https://blog.csdn.net/u012672646/article/details/56676804 本地修改了一些文件,其中包含修改.新增.删除的,不需要了想要丢弃,于是做 ...