国产精品,日韩av,hdsexvideos中国少妇,xxxxx性bbbbb欧美熟妇,波多野结衣乳巨码无在线,国产成人亚洲精品乱码

自定義函數(shù)-List類型

2020-12-24 09:03:02 1168 本站

List - 集合類型,使用時用[]封裝,中間數(shù)據(jù)用,分隔

定義List:List list = []

例:

List colors = ["red","blue","green","yellow"]

List類型的方法:

  • list.add(<Object any_type>):向列表添加元素

      返回值類型:Boolean

      例:

List colors = ["red", "blue", "green"]

colors.add("yellow") //返回:true

  • list.addAll(<List list1>):向列表添加多個元素

      返回值類型:Boolean

      例:

List colors = ["red", "blue"]

colors.addAll(["green", "yellow"]) //返回:true

  • list.clear():清空字符串

      返回值類型:無返回值

      例:

List colors = ["red", "blue"]

colors.clear()

  • list.contains(<Object any_type>):判斷列表中是否存在指定元素。如果列表中存在元素, 則返回"true"

      返回值類型:Boolean

      例:

List colors = ["red", "blue", "green", "yellow"]

result = colors.contains("red") //返回:true

  • list.containsAll(<List list1>):判斷列表中是否存在所有指定元素。如果列表中存在元素, 則返回"true"

      返回值類型:Boolean

      例:

List colors = ["red", "blue", "green"]

result = colors.containsAll(["red", "yellow"]) //返回:false

  • list.indexOf(<Object any_type>):返回給定元素在列表中的位置 (第一個元素的位置為 "0")

      返回值類型:BigDecimal

      例:

 List colors = ["red", "blue", "green", "yellow"]

 result = colors.indexOf("green") //返回:2

  • list.get(<BigDecimal indexNumber>):返回給定位置在列表中的元素 (第一個元素的位置為 "0")

      返回值類型:Object

      例:

 List colors = ["red", "blue", "green", "yellow"]

 result = colors.get(1) // 返回: "blue"

  • list.size():返回列表中元素的數(shù)目

      返回值類型:BigDecimal

      例:

List colors = ["red", "blue", "green", "yellow"]

result = colors.size() //返回:4

  • list.sort(<Boolean bool>)列表排序,可選布爾值指定升序 (true)/降序 (false),為空時默認(rèn)為升序

      返回值類型:List

      例:

List colors = ["red", "blue", "green", "yellow"]

colors.sort() // 返回: ["blue","green","red","yellow"]

  • list.subList(<BigDecimal startIndex>,<BigDecimal endIndex>) 根據(jù)指定的開始和結(jié)束索引 (包含指定的索引,不包括結(jié)束索引) 從給定列表中返回一組元素

      返回值類型:List

      例:

List colors = ["red", "blue", "green", "yellow"]

result = colors.subList(1, 3); // 返回: ["blue","green"]

  • list.remove(<BigDecimal index>)移除并返回指定索引處的元素 (第一個元素的索引為 "0")

      返回值類型:Object

      例:

 List colors = ["red", "blue", "green", "yellow"]

 colors.remove(2) // 返回: "green"

  • list.isEmpty()判斷列表是否為空。返回一個布爾值-如果列表中不包含任何值, 則為true; 否則為-如果列表中包含值, 則為false

      返回值類型:Boolean

      例:

 List colors = ["red", "blue", "green", "yellow"]

 result = colors.isEmpty() // 返回: false

  • list.intersect(<List list>) 返回指定列表與當(dāng)前列表的交集

      返回值類型:List

      例:

 colors = ["red", "blue", "green", "orange"]

 fruits = ["apple","orange","banana"]

 result = colors.intersect(fruits) // 返回:["orange"]

  • list.lastIndexOf(<Object any_type>) 返回指定元素在列表中的最后一個匹配項(xiàng)的位置

      返回值類型:BigDecimal

      例:

  colors = ["red", "blue", "green", "yellow", "green"]

  result = colors.lastIndexOf("green") //返回:4

  • list.eachWithIndex(<Closure closure>) 遍歷列表中的數(shù)據(jù),閉包中傳入item和index

      返回值類型:List

      例:

  List list = ["a", "c"]

  list.eachWithIndex { item, int i ->

      log.info(item)

      log.info(i)

  } //運(yùn)行日志:a  0  b  1

  • list.each(<Closure closure>) 遍歷列表中的數(shù)據(jù),閉包中傳入item

      返回值類型:List

      例:

  List list = ["a", "c"]

  list.each { item -> 

      log.info(item)

  } //運(yùn)行日志:a   b

  • list.any{<Closure closure>} 判斷是否至少有一個元素有效

      返回值類型:Boolean

      例1:

  List list = ["a", "b"]

  Boolean b = list.any{x ->x=="a"} //list中是否有一個元素為"a"

  log.info(b)

  } //運(yùn)行日志:true

      例2:

  List list = [["a":1], ["a":2]]

  Boolean b = list.any{x ->(x.a as Integer)>1} //list中元素為map,并且key為"a"的value值是否有一個大于1

  log.info(b)

  } //運(yùn)行日志:true

  • list.collect{<Closure closure>} list中的元素為map,取出每個map中的value值

      返回值類型:List

      例:

  List list = [["a":1], ["a":2]]

  List list = list.collect{x ->x.a} 

  log.info(list)

  } //運(yùn)行日志:[1,2]

  • list.find{<Closure closure>} list中的元素為map,取出每個map中第一個符合條件的value值

      返回值類型:Map

      例:

  List list = [["a":1], ["a":2]]

  Map map = list.find{x ->(x.a as Integer)>1} 

  log.info(map)

  } //運(yùn)行日志:{a=2}



部分內(nèi)容來源于互聯(lián)網(wǎng),如有侵權(quán),請聯(lián)系客服刪除處理。
在線咨詢 撥打電話