• groovy学习(一)列表


    numbers = [11, 12, 13, 14]
    println(numbers[0])
    println(numbers[3])
    println(numbers[-1])//最左边的元素
    println(numbers[-3])//最左边的第3元素
    println(numbers[0..2])//第0, 1, 2元素
    println(numbers[1..<3])//第1,2个元素

    numbers[1] = 22//给第1个元素赋值
    println(numbers)

    numbers[1] = [33, 44]//将第1个元素替换为列表,numbers变成列表的列表
    println(numbers)

    numbers << 15
    println(numbers) //添加到最后

    numbers = [11, 12, 13, 14]
    numbers += [15, 16] //连接列表
    println(numbers)

    numbers = [11, 12, 13, 14]
    numbers.add(15) //注意这里返回的是true,表示添加成功
    println(numbers)

    numbers = [11, 12, 13, 14]
    numbers.add(2, 15) //第2个位置添加15,第2个以及以后的元素住后移
    println(numbers)

    numbers = [11, 12, 13, 14]
    println(numbers.get(1))//返回第1个位置的元素

    numbers = [11, 12, 13, 14]
    println(numbers.isEmpty())//是否为空

    numbers = [11, 12, 13, 14]
    println(numbers.size())//长度

    println([11, 12, [13, 14]].flatten()) //嵌套的列表转化成一个同有嵌套的列表

    numbers = [11, 12, 13, 14]
    println(numbers.getAt(1))//返回第一个元素

    numbers = [11, 12, 13, 14]
    println(numbers.getAt(1..2))//返回第1到2个元素

    numbers = [11, 12, 13, 14]
    println(numbers.getAt([0, 2, 3]))//返回第0, 2, 3个元素

    numbers = [11, 12, 13, 14]
    println(numbers.intersect([13, 14, 15, 11]))//取交集

    numbers = [11, 12, 13, 14]
    println(numbers.pop())//返回最后一个元素,相当于栈顶

    numbers = [11, 12, 13, 14]
    println(numbers.reverse()) //逆序

    numbers = [14, 13, 11, 12]
    println(numbers.sort()) //排序

    numbers = [14, 13, 11, 12]
    println(numbers.remove(3))//返回删除的第3个元素
    println(numbers)

    numbers = ['Ken', 'John', 'Sally', 'Jon']
    println(numbers.remove('Ken')) //Ken在列表中返回true,不在返回false
    println(numbers)
  • 相关阅读:
    149. Max Points on a Line(js)
    148. Sort List(js)
    147. Insertion Sort List(js)
    146. LRU Cache(js)
    145. Binary Tree Postorder Traversal(js)
    144. Binary Tree Preorder Traversal(js)
    143. Reorder List(js)
    142. Linked List Cycle II(js)
    141. Linked List Cycle(js)
    140. Word Break II(js)
  • 原文地址:https://www.cnblogs.com/zhouyang209117/p/4899385.html
Copyright © 2020-2023  润新知