vallst6=lst5.toList //把可变list 转变数组用toArray vallst7=lst5.toArray }
.toArray:集合转数组 .toBuffer[B >: A]:返回缓冲区,包含了 Map 的所有元素 .toList:返回 List,包含了Map的所有元素 .toSeq:返回 Seq,包含了Map的所有元素 . toSet:返回 Set,包含了 Map 的所有元素 .toString():返回字符串对象 元组 与列表一样,元组也是不可变的,但与列表不同的是元组可以包含不同类...
List(1,2,3,4,-5,6,7) takeWhile(_>0)//结果:res1: List[Int] = List(1, 2, 3, 4)List(1,2,3,4,-5,6,7) dropWhile(_>0)//结果:res2: List[Int] = List(-5, 6, 7)List(1,2,3,4,-5,6,7) span (_>0)//结果:res3: (List[Int], List[Int]) = (List(1, 2, 3, ...
prepend(1, 2, 3) //在头部添加元素 list.append(4,5) //在尾部添加元素 list.insert(5,6) //在位置5处添加元素 0 +=: list += 7 += 8 //在前面添加0,后面添加7、8 //合并两个list val list09 = list ++ list9 //将list和list9相连接 list09 ++= list99 //将list99追加到list09 //...
相互转换 toList和toListBuffer 集合 不可变的 val set = Set[Int](1,2,3) set.head获取第一个元素 set.tail 获取剩下的元素 set.isEmpty判断是否为空 可变的 HashSet 增加删除元素和List一样的方法 映射 哈希表相当于java的map,定义的方式 val map=Map("a"->1,("b",3),"c"->Array[Int](4,5...
\ genericBuilder prefixLength to << groupBy prepend toArray WithFilter grouped prependAll toBuffer addString hasDefiniteSize prependToList toIndexedSeq aggregate hashCode product toIterable andThen head reduce toIterator append headOption reduceLeft toList appendAll indexOf reduceLeftOption toMap apply ...
数组(Array)、链表(List)、Set、Map、Tuple。 1、数组Array 对于数组的使用,若想调用数组提供的方法,我们需要使用 import Array._引入包。 1.数组的声明 1.数组Array分为定长和变长两种数组。后期常用的类型是定长(Array)。 2.Scala的泛型的声明使用[]来定义的,不同于Java的<>。
objectMainextendsApp{traitAnimal{defspeak:String}classDogextendsAnimal{defspeak="Woof!"}classCatextendsAnimal{defspeak="Meow!"}classParrotextendsAnimal{defspeak="Squawk!"}classAnimalShelter[A<:Animal]{privatevaranimals:List[A] =NildefaddAnimal(animal:A):Unit= { ...
Java集合有三大类型List、Set、Map,从图中可以发现类型系统设计并不统一,Map和其它两者进行转换也比较复杂;Scala在该方面进行了改进,Scala中集合也对应的分三种类型:序列(Seq)、集(Set)、映射(Map),所有集合统一的扩展自Iterable特质。 Scala为大多数集合类提供了可变和不可变版本,大体思想和Java中的不可变String和可...
不可变转可变(toBuffer) val list=List[Int](23,44,23,56,5) val bufferList: mutable.Buffer[Int] = list.toBuffer 可变List创建可变List通过apply方法: ListBuffer[元素类型](初始元素,...)val list=ListBuffer[Int](1,2,3,4,5) 查看ListBuffer支持哪些api 需要导包...