Scala-arrays, collections, tuples and files

Array

One, one-dimensional array declaration

var array = new Array[Int](10)
var array : Array[Int] = {}
var array = Array()
import scala.collection.mutable._
var array = ArrayBuffer[Int]()

Second, the declaration of multi-dimensional array

var array = Array.ofDim[Int](3,4)
var array = new Array[Array[Int]](10)

Map

1. Variable Map

import scala.collection.mutable._
var map = Map("Destiny" -> 1,"Freedom" -> 2)
var map = Map(("Destiny",1),("Freedom",2))

Second, the immutable Map

import scala.collection.immutable._
var map = Map("Destiny" -> 1,"Freedom" -> 2)
var map = scala.collection.immutable.Map(("Destiny",1),("Freedom",2))

Three, Map operation

//获取Map的Value
map("Freedom")
map.get("Freedom")
map.getOrElse("Freedom",-1)
//判断key是否存在
map.contains("Freedom")
//添加或移除新的元素
map += "Fate" -> 3
map -= "Fate" -> 3 
//更新Map的Value
map("Freedom") = 1
//Map的迭代
map.foreach(println)
for(s <- map)
println(s)

Collection (List)

First, the immutable List

var list = List(1,2,3)
var nulList : List[Nothing] = List()
var dim : List[List[Int]] = List(List(1,2,3),List(4,5,6))

//查看第一个元素
list.head
//查看除第一个元素外的元素
list.tail

Two, variable LinkedList

import scala.collection.mutable._
var linkedList = LinkedList(1,2,3)

while(linkedList != Nil){
	//当前元素乘以2
	linkedList.elem = linkedList.elem * 2
	//移动指针到下一个元素
	linkedList = linkedList.next
}
//高阶函数
linkedList.map(_*2)

sequence

1. Vector

Vector is a sequence with subscripts, you can access the elements in Vector (immutable) through subscripts

var vector = Vector(1,2,3)

Second, Range

Vector is a sequence of integers

Range(0,5)
println(0 until 5)
print(0 to 5)
('0' to '9') ++ ('A' to 'Z')
//转成List
1 to 5 toList

Collection

1. Set

A collection of non-repeating elements (immutable)

var s = Set(1,2,3)

二、SortedSet

Sortable, variable

import scala.collection.mutable._
var sortedSet = SortedSet(1,2,3)
//判断元素是否存在
sortedSet.contains(1)
//集运算
var s1 = Set(1,2,3,4,5,6)
var s2 = Set(5,6,7,8,9,10)
//并集
s1 union s2
//交集
s1 interset s2
//补集
s1 diff s2

Tuple

1. Tuple's statement

var tuple = Tuple3("Destiny","Freedom",1)
var tuple = ("Destiny","Freedom",1)

Second, the operation of Tuple

//获取某索引的值
tuple._1
//Tuple的迭代
tuple.productIterator.foreach(println)

File operations

import java.io.{File, FileInputStream, PrintWriter}
import scala.io.Source._
object FileDemo {
  
  def main(args: Array[String]): Unit = {
    var source = fromFile("C:\\Users\\Administrator\\Desktop\\log.txt","GB2312")

    //将文件作为字符串输出
    println(source.mkString)

    //将文件的每一行读入并输出
    var line = source.getLines()
    line.foreach(println)

    //读取字符
    for (s <- source) println(s)

    //读取URL
    var url = fromURL("http://www.baidu.com","UTF-8")
    println(url.mkString)

    //Scala不支持读取二进制文件
    //通过调用java的InputStream来实现
    var file = new File("C:\\Users\\Administrator\\Desktop\\log.txt");
    //实例化FileInputStream
    var in = new FileInputStream(file)
    //创建一个buffer数组
    var buffer = new Array[Byte](file.length().toInt)
    //读取文件
    println(in.read(buffer))
    //关闭流
    in.close()

    //通过java来实现写文件
    var out = new PrintWriter("C:\\Users\\Administrator\\Desktop\\log.txt")
    for (s <- 0 until 10) out.println(s)
    //关闭流
    out.close()
  }
}
Published 131 original articles · won 12 · 60,000 views +

Guess you like

Origin blog.csdn.net/JavaDestiny/article/details/92412538