zoukankan      html  css  js  c++  java
  • Dart list add()和addAll方法使用

    可变列表可以在运行时动态增长。所述 List.add() 函数将指定值到列表的结尾,并返回修改的列表对象。下面说明了相同的内容。

     List l = [1,2,3];
     l.add(12);
     print(l);
    }

    它将产生以下 输出:

    [1, 2, 3, 12]

    所述 List.addAll() 函数接受由逗号分隔的多个值和这些附加到列表。

     List l = [1,2,3];
     l.addAll([12,13]);
     print(l);
    }

    它将产生以下 输出:

    [1, 2, 3, 12, 13]

    所述 List.addAll() 函数接受由逗号分隔的多个值和这些附加到列表。

    void main() {
       List l = [1,2,3];
       l.addAll([12,13]);
       print(l);
    }
    

    它将产生以下 输出

    [1, 2, 3, 12, 13]


    Dart还支持在List中的特定位置添加元素。所述 insert() 函数接受一个值和指定的索引处将其插入。类似地, insertAll() 函数从指定的索引开始插入给定的值列表。insert和 insertAll 函数的语法如下所示

    List.insert(index,value)
    List.insertAll(index, iterable_list_of _values)
    

    以下示例分别说明了 insert() 和 insertAll() 函数的 用法 。

    句法

    List.insert(index,value)  
    List.insertAll([Itearble])
    

    示例:List.insert()

    void main() {
     List l = [1,2,3];
     l.insert(0,4);
     print(l);
    }
    

    它将产生以下 输出

    [4, 1, 2, 3]
    

    示例:List.insertAll()

    void main() {
     List l = [1,2,3];
     l.insertAll(0,[120,130]);
     print(l);
    }
    

    它将产生以下 输出

    [120, 130, 1, 2, 3]
  • 相关阅读:
    迭代器
    闭包函数与装饰器
    pyhton的函数
    文件处理
    Python2和Python3编码的区别
    hdu5080:几何+polya计数(鞍山区域赛K题)
    hdu1695:数论+容斥
    HDU3564 --- Another LIS (线段树维护最值问题)
    hdu1521:排列组合---指数型母函数
    poj1014:母函数+优化
  • 原文地址:https://www.cnblogs.com/maqingyuan/p/13730289.html
Copyright © 2011-2022 走看看