前言
实际应用中,需要对List() 中 拼接字符串中的空值进行删除操作。
程序
object test_List_filter {
def main(args: Array[String]): Unit = {
// val cell_grid_lists :List[String] = List("cell/level/type/grid","cell0/level0/type0/grid0","cell1/level1/type1/")
val cell_grid_lists :List[String] = List("cell/level/type/g","cell0/level0/type0/","cell1/level1/type1/")
var cell_grid_lists_filter = hanle_null(cell_grid_lists)
println("方法处理后的结果:" + cell_grid_lists_filter)
if(cell_grid_lists_filter.size < 1 ){
cell_grid_lists_filter = null
println("null")
}else{
println("not null")
}
if (cell_grid_lists_filter == null){
println("kongkong")
}
}
def hanle_null(cell_grid_lists: List[String]):List[String]={
var cell_grids :List[String] = List()
cell_grid_lists.foreach(x=>{
val cell_grid_list= x.split("/")
if (cell_grid_list.size > 3) {
cell_grids +:= cell_grid_list.mkString("/") // cell_grids 存放的是网格信息和null
}else{
cell_grids +:= null
}
})
// cell_grids 存放的是网格信息和null,删除null信息或者为null信息
cell_grids = cell_grids.filter(_ != null)
println(cell_grids)
cell_grids
}
}
结果显示
List(cell/level/type/g)
方法处理后的结果:List(cell/level/type/g)
not null