scala 空列表_如何在Scala中展平列表列表?
scala 空列表
Flattening of List is converting a list of multiple List into a single List. To flatten List of List in Scala we will use the flatten method.
扁平化列表是將多個列表的列表轉換為單個列表。 為了在Scala中扁平化List列表,我們將使用flatten方法。
Syntax:
句法:
val newList = list.flattenProgram to flatten a list of list with numerical values
程序用數值拼合列表列表
object MyClass {def main(args: Array[String]) {val ListofList = List(List(214, 56), List(6, 12, 34, 98))println("List of List: " + ListofList)println("Flattening List of List ")val list = ListofList.flattenprintln("Flattened List: " + list)} }Output
輸出量
List of List: List(List(214, 56), List(6, 12, 34, 98)) Flattening List of List Flattened List: List(214, 56, 6, 12, 34, 98)The same flatten method can also be applied to convert sequence of sequence to a single sequence(Array, list, Vector, etc.).
同樣的展平方法也可以應用于將序列序列轉換為單個序列( 數組 ,列表,向量等)。
You can convert list of lists to list of type strings in two ways.
您可以通過兩種方式將列表列表轉換為類型字符串列表 。
Let's explore how to?
讓我們探索如何?
Program:
程序:
object MyClass {def main(args: Array[String]) {val ListofList = List(List("Include", "Help"), List("Programming", "Tutorial"))println("List of List: " + ListofList)println("Flattening List of List ")val list = ListofList.flattenprintln("Flattened List: " + list)} }Output
輸出量
List of List: List(List(Include, Help), List(Programming, Tutorial)) Flattening List of List Flattened List: List(Include, Help, Programming, Tutorial)Another way could split the list of list into character lists.
另一種方法可以將列表列表拆分為字符列表。
Program:
程序:
object MyClass {def main(args: Array[String]) {val ListofList = List("Include", "Help")println("List of List: " + ListofList)println("Flattening List of List ")val list = ListofList.flattenprintln("Flattened List: " + list)} }Output
輸出量
List of List: List(Include, Help) Flattening List of List Flattened List: List(I, n, c, l, u, d, e, H, e, l, p)翻譯自: https://www.includehelp.com/scala/how-to-flatten-a-list-of-list-in-scala.aspx
scala 空列表
總結
以上是生活随笔為你收集整理的scala 空列表_如何在Scala中展平列表列表?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: treeset java_Java Tr
- 下一篇: scala中map添加值_如何在Scal