Scala学习(集合01)

Posted 数据攻城小狮子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Scala学习(集合01)相关的知识,希望对你有一定的参考价值。

文章目录

数组

不可变数组

package learn04
object ScalaCollection 
  def main(args: Array[String]): Unit = 
    //数组定义
    val arr01=new Array[Int](4)
    println(arr01.length)//4
    //数组赋值
    //修改某个元素的值
    arr01(3)=10
    val i=10
    arr01(i/3)=20
    //采用方法的形式修改数组的值
    arr01.update(0,1)
    //遍历数组
    //查看数组
    println(arr01.mkString(","))
    //普通遍历
    for(i<- arr01)
      println(i)
    
    //简化遍历
    def printx(elem:Int):Unit=
      println(elem)
    
    arr01.foreach(printx)
    arr01.foreach((x)=>println(x))
    arr01.foreach(println(_))
    arr01.foreach(println)
  

4
1,0,0,20
1
0
0
20
1
0
0
20
1
0
0
20
1
0
0
20
1
0
0
20
package learn04
object ScalaCollection
  def main(args: Array[String]): Unit = 
    //创建数组的另外一种方式
    val arr1=Array(1,2,3,4)
    val arr2=Array(5,6,7,8)
    //添加数组元素,创建新数组
    val arr3:Array[Int]=arr1:+4
    print("arr3:")
    println(arr3.mkString(","))
    println(arr1 eq arr3)
    val arr4:Array[Int]=arr1++:arr2
    //添加集合
    val arr5:Array[Int]=arr1++arr2
    print("arr4:")
    println(arr4.mkString(","))
    print("arr5:")
    println(arr5.mkString(","))
    //多维数组
    var myMatrix=Array.ofDim[Int](3,3)
    myMatrix.foreach(list=>list.foreach(println))
    //合并数组
    val arr6:Array[Int]=Array.concat(arr1,arr2)
    println(arr6.mkString(","))
    //创建指定范围的数组
    val arr7:Array[Int]=Array.range(0,2)
    println(arr7.mkString(","))
    val arr8:Array[Int]=Array.fill[Int](5)(-1)
    println(arr8.mkString(","))
  

arr3:1,2,3,4,4
false
arr4:1,2,3,4,5,6,7,8
arr5:1,2,3,4,5,6,7,8
0
0
0
0
0
0
0
0
0
1,2,3,4,5,6,7,8
0,1
-1,-1,-1,-1,-1

可变数组

package learn04
import scala.collection.mutable.ArrayBuffer
object demo01 
  def main(args: Array[String]): Unit = 
    val buffer=new ArrayBuffer[Int]
    //增加数组
    buffer.append(1,2,3,4)
    println(buffer.mkString(","))
    //修改数据
    buffer.update(0,5)
    buffer(1)=6
    println(buffer.mkString(","))
    //删除数据
    buffer.remove(2)
    println(buffer.mkString(","))
    //查询数据
    println(buffer(1))
    //循环集合
    for(i <- buffer)
      print(i+" ")
    
  

1,2,3,4
5,6,3,4
5,6,4
6
5 6 4 
package learn04
import scala.collection.mutable.ArrayBuffer
object demo01 
  def main(args: Array[String]): Unit = 
    val buffer1=ArrayBuffer(1,2,3,4)
    val buffer2=ArrayBuffer(5,6,7,8)
    val buffer3:ArrayBuffer[Int]=buffer1+=5
    print("buffer3:")
    println(buffer3.mkString(","))
    println(buffer1 eq buffer3)
    //使用++运算符会产生新的集合数组
    val buffer4:ArrayBuffer[Int]=buffer1++buffer2
    print("buffer4:")
    println(buffer4.mkString(","))
    //使用++=运算符会更新之前的集合,不会产生新的数组
    val buffer5:ArrayBuffer[Int]=buffer1++=buffer2
    print("buffer5:")
    println(buffer5.mkString(","))
    println(buffer1 eq buffer4)
    println(buffer1 eq buffer5)
  

buffer3:1,2,3,4,5
true
buffer4:1,2,3,4,5,5,6,7,8
buffer5:1,2,3,4,5,5,6,7,8
false
true

可变数组和不可变数组转换

package learn04
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
object demo02 
  def main(args: Array[String]): Unit = 
    val buffer=ArrayBuffer(1,2,3,4)
    val array=Array(4,5,6,7)
    //将不可变数组转换为可变数组
    val buffer1:mutable.Buffer[Int]=array.toBuffer
    //将可变数组转换为不可变数组
    val array1:Array[Int]=buffer.toArray
  

Seq集合

不可变List

package learn04
object demo03 
  def main(args: Array[String]): Unit = 
    //Seq集合
    val list=List(1,2,3,4)
    println("list:",list)
    //增加数据
    val list1:List[Int]=list:+1
    println("list1:",list1)
    println(list1 eq list)
    list1.foreach(print)
    val list2:List[Int]=1+:list
    println()
    print("list2:")
    list2.foreach(print)
    val list3:List[Int]=list.updated(1,5)
    println()
    println(list eq list3)
    print("list3:")
    list3.foreach(print)
  

(list:,List(1, 2, 3, 4))
(list1:,List(1, 2, 3, 4, 1))
false
12341
list2:11234
false
list3:1534
package learn04
object demo04 
  def main(args: Array[String]): Unit = 
    //Seq集合
    val list1=List(1,2,3,4)
    //空集合
    val list2:List[Nothing]=List()
    val nil=Nil
    println(list2.mkString(","))
    println(list2 eq nil)
    //创建集合
    val list3:List[Int]=1::2::3::Nil
    val list4:List[Int]=list1:::Nil
    println(list3.mkString(","))
    println(list4.mkString(","))
    //连接集合
    val list5:List[Int]=List.concat(list3,list4)
    println(list5.mkString(","))
    //创建一个指定重复数量的元素列表
    val list6:List[String]=List.fill[String](3)("a")
    println(list6.mkString(","))
  


true
1,2,3
1,2,3,4
1,2,3,1,2,3,4
a,a,a

可变List

package learn04
import scala.collection.mutable.ListBuffer
object demo05 
  def main(args: Array[String]): Unit = 
    //可变集合
    val buffer=new ListBuffer[Int]()
    //增加数据
    buffer.append(1,2,3,4)
    //修改数据
    buffer.update(1,3)
    //删除数据
    println(buffer.mkString(","))
    buffer.remove(2,2)//从下标2开始,删除2个元素
    //遍历集合
    buffer.foreach(println)
  

1,3,3,4
1
3
package learn04
import scala.collection.mutable.ListBuffer
object demo06 
  def main(args: Array[String]): Unit = 
    //可变集合
    val buffer1=ListBuffer(1,2,3,4)
    val buffer2=ListBuffer(5,6,7,8)
    //增加数据
    val buffer3:ListBuffer[Int]=buffer1:+5
    val buffer4:ListBuffer[Int]=buffer1+=5
    val buffer5:ListBuffer[Int]=buffer1++buffer2
    val buffer6:ListBuffer[Int]=buffer1++=buffer2
    println(buffer3.mkString(","))
    println(buffer4.mkString(","))
    println(buffer5.mkString(","))
    println(buffer6.mkString(","))
    println(buffer5 eq buffer1)
    println(buffer6 eq buffer1)
    val buffer7:ListBuffer[Int]=buffer1-2
    val buffer8:ListBuffer[Int]=buffer1-=2
    println(buffer7 eq buffer1)
    println(buffer8 eq buffer1)
  

1,2,3,4,5
1,2,3,4,5,5,6,7,8
1,2,3,4,5,5,6,7,8
1,2,3,4,5,5,6,7,8
false
true
false
true

可变集合和不可变集合转换

package learn04
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
object demo07 
  def main(args: Array[String]): Unit = 
    val buffer=ListBuffer(1,2,3,4)
    val list=List(5,6,7,8)
    //可变集合转变为不可变集合
    val list1:List[Int]=buffer.toList
    //不可变集合转变为可变集合
    val buffer1:mutable.Buffer[Int]=list.toBuffer
  

Set集合

不可变Set

package learn04

object demo08 
  def main(args: Array[String]): Unit = 
    val set1=Set(1,2,3,4)
    val set2=Set(5,6,7,8)
    //增加数据
    val set3:Set[Int]=set1+5
    val set4:Set[Int]=set1+(5,6,7,8)
    println(set1 eq set3)
    println(set1 eq set4)
    set4.foreach(print)
    //删除数据
    val set5:Set[Int]=set1-2-3
    println()
    set5.foreach(print)
    val set6:Set[Int]=set1++set2
    println()
    set6.foreach(print)
    val set7:Set[Int]=set2++:set1
    println()
    set7.foreach(print)
    println()
    println(set6 eq set7)
  

false
false
51627384
14
51627384
51627384
false

可变Set

package learn04
import scala.collection.mutable
object demo09 
  def main(args: Array[String]): Unit = 
    val set1=mutable.Set(1,2,3,4)
    val set2=mutable.Set(5,6,以上是关于Scala学习(集合01)的主要内容,如果未能解决你的问题,请参考以下文章

scala 集合通用方法

Spark基础-scala学习(集合)

Scala 学习 -- 其他集合类学习

Scala集合学习总结

Scala学习——集合

Scala 学习笔记之集合