Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Lesson 32: the basic operation of List and the implementation of List sorting algorithm based on pattern matching

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

package com.dt.scala.datasetobject HelloList { def main(args: Array[String]): Unit = { //define List, directly use applay method val bigData = List("Spark","Hadoop") val data = List(1,2,3) //use:: definition list val bigData_core = "Spark"::"Hadoop"::Nil //:: Nil cannot be omitted,"Spark"::"Hadoop" syntax is wrong //Nil is an empty list and:: is a method of the right element, e.g."Hadoop"::Nil :: is a method of Nil //equivalent to val bigData_core2 = Nil.:: ("Hadoop") val data_Int = 1::2::3::Nil //merge two lists using::: method val data_union = data ::: data_Int //Determine if the list is empty data.isEmpty //get the head of the list data.head //For List, the first element is called head, and all other elements are called tail data.tail.head //List elements in advance val List(a,b) = bigData //a="Spark" , b="Hadoop" println("a = "+a+" b = "+b) //If there are multiple elements in the List, you can extract them as follows val first::second::rest = data //rest represents the rest of the List, the type returned is List println("first:"+first+" ==== "+"second:"+second+" ==== "+"rest:"+rest) //first:1 ==== second:2 ==== rest:List(3) //Sort List using pattern matching val shuffleData = List(9,3,2,10,3,34,1) def compute(data : Int,dataSet : List[Int]) : List[Int] = dataSet match { case List() => List(data) //if dataSet is empty, return List(data) case head :: tail => { //if dataSet is not empty if (data compute(head,sortList(tail)) } println(sortList(shuffleData)) }}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report