美文网首页
3、列表List、映射Map、范围Range

3、列表List、映射Map、范围Range

作者: 最美下雨天 | 来源:发表于2019-03-11 09:48 被阅读0次

    列表List

    列表

    常用方法:

    //List定义
    def list=new ArrayList()//这是java的定义方式,这里也可以用
    
    def list2=[1,2,3,4,5]
    println list2.class //有没有像数组,输出:java.util.ArrayList
    
    def list3=[1,2,3,4,5] as int[]
    println list3.class //输出:[I   说明这个是数组
    
    int[] array=[1,2,3,4,5]//数组
    
    //添加元素
    list2.add(6)
    list2.leftShift(7)//相当于add
    println list2+8  //相当于add   输出:[1, 2, 3, 4, 5, 6, 7, 8]  但是会产生一个新列表,原列表不变
    
    println list2.toListString()// 输出:[1, 2, 3, 4, 5, 6, 7]
    
    
    //删除元素
    list2.remove(1) //可以传角标也可以传对象
    list2.remove((Object)4)//输出:[1, 3, 5, 6, 7]
    println list2
    
    list2.removeAt(2)
    println list2   //输出:[1, 3, 6, 7]
    
    list2.removeElement(3)
    println list2 //输出:[1, 6, 7]
    
    list2.removeAll {
        it%2==0
    }
    println list2 //输出:[1, 7]
    
    def list5=[1,2,3,4,5]
    println  list5-[2,4]  //输出:[1, 3, 5]
    println list5.toListString()
    
    
    //排序
    def sortList=[6,-1,3,5,2,-9]
    
    //可以像java一样定义一个比较器
    Comparator comparator={a,b->
        a==b?0:(Math.abs(a)>Math.abs(b)?1:-1)
    }
    
    sortList.sort(comparator)
    //同上
    //Collections.sort(sortList,comparator)
    println sortList
    
    
    sortList.sort {a,b->
        a==b?0:(Math.abs(a)>Math.abs(b)?1:-1)
    }
    
    def sortStringList=['abc','z','hello','groovy','java']
    sortStringList.sort{
        it.size()
    }
    println sortStringList//输出:[z, abc, java, hello, groovy]
    
    
    //查找
    def findList = [-3, 9, 6, 2, -7, 1, 5]
    int result = findList.find { return it % 2 == 0 }
    //def result = findList.findAll { return it % 2 != 0 }
    //def result = findList.any { return it % 2 != 0 }
    //def result = findList.every { return it % 2 == 0 }
    println result
    println findList.min { return Math.abs(it) }
    println findList.max { return Math.abs(it) }
    //统计个数
    def num = findList.count { return it % 2 == 0 }
    println num
    

    映射Map

    //java中的定义方式
    //def map = new HashMap()
    def colors = [red  : 'ff0000',
                  green: '00ff00',
                  blue : '0000ff']
    //索引方式
    //println colors['red']
    println colors.red
    colors.blue
    //添加元素
    //colors.yellow = 'ffff00'
    colors.complex = [a: 1, b: 2]
    
    //获取colors的类型,注意不能用colors.class来获取,colors.class相当于是从colors中查找key为class的value了
    //println colors.getClass()
    /**
     * Map操作详解
     */
    def students = [
            1: [number: '0001', name: 'Bob',
                score : 55, sex: 'male'],
            2: [number: '0002', name: 'Johnny',
                score : 62, sex: 'female'],
            3: [number: '0003', name: 'Claire',
                score : 73, sex: 'female'],
            4: [number: '0004', name: 'Amy',
                score : 66, sex: 'male']
    ]
    
    //遍历Entry
    students.each { def student ->
        println "the key is ${student.key}, " +
                " the value is ${student.value}"
    }
    //带索引的遍历
    students.eachWithIndex { def student, int index ->
        println "index is ${index},the key is ${student.key}, " +
                " the value is ${student.value}"
    }
    //直接遍历key-value
    students.eachWithIndex { key, value, index ->
        println "the index is ${index},the key is ${key}, " +
                " the value is ${value}"
    }
    //Map的查找
    def entry = students.find { student ->
        return student.value.score >= 60
    }
    //println entry
    
    def entrys = students.findAll { student ->
        return student.value.score >= 60
    }
    //println entrys
    
    def count = students.count { student ->
        return student.value.score >= 60 &&
                student.value.sex == 'male'
    }
    //println count
    def names = students.findAll { student ->
        return student.value.score >= 60
    }.collect {
        return it.value.name
    }
    //println names.toListString()
    
    def group = students.groupBy { student ->
        return student.value.score >= 60 ? '及格' : '不及格'
    }
    //println group.toMapString()
    
    /**
     * 排序,这个会生成一个新map,原map是不变的
     */
    def sort = students.sort { student1, student2 ->
        Number score1 = student1.value.score
        Number score2 = student2.value.score
        return score1 == score2 ? 0 : score1 < score2 ? -1 : 1
    }
    
    println sort.toMapString()
    
    println students.toMapString()
    
    
    输出结果

    Range

    image.png
    //范围Range是继承自List的,所以前面讲解到的List中的方法在Range中都可以使用
    def range=1..10
    
    //输出
    println range[0]  //1
    println range.contains(10)  //true
    
    println range.from  //1
    println range.to  //10
    
    //遍历
    range.each {
        println it
    }
    
    for(i in range)
    {
        println i
    }
    
    //Range的典型使用
    def result=getGrade(70)
    println result
    
    def getGrade(def score)
    {
        def result;
        switch (score)
        {
            case 0..<60://从0到60,但是小于60,也就是说不包括60
                result="不及格"
                break
            case 60..<70:
                result='及格'
                break
            case 70..<80:
                result='良好'
                break
            case 80..100:
                result='优秀'
                break
        }
        return  result
    }
    
    
    

    相关文章

      网友评论

          本文标题:3、列表List、映射Map、范围Range

          本文链接:https://www.haomeiwen.com/subject/dnecpqtx.html