首页 > 其他分享 >无涯教程-Dart - Inserting Elements into a List函数

无涯教程-Dart - Inserting Elements into a List函数

时间:2023-11-15 19:31:46浏览次数:37  
标签:Inserting insert Elements into List 12 insertAll print main

可变列表可以在运行时动态增长, List.add()函数将指定的值附加到List的末尾,并返回修改后的List对象 ,如下所示。

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

它将产生以下输出-

[1, 2, 3, 12]

List.addAll()函数接受多个用逗号分隔的值,并将它们附加到列表中。

void main() { 
   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还支持在列表中的特定位置添加元素, 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]

参考链接

https://www.learnfk.com/dart-programming/dart-programming-inserting-elements-into-list.html

标签:Inserting,insert,Elements,into,List,12,insertAll,print,main
From: https://blog.51cto.com/u_14033984/8401093

相关文章

  • 控件库主题 HML.SkinTool
    主题框架介绍该文章详解HML控件库12.0.0.0版本,主题开发框架部分。下面将讲解如何在主题框架下开发使用控件和完全删除主题功能部分代码。下图为主题框架运作原理:  由上面结构图看出主题文件都是以XML文件格式保存,XML内容保存节点结构为“Skin/主题对象所在程序集名......
  • Error: install profile containers-default-0.50.1: generate default profile into
    Bug#2040082“errorparsingAppArmorversion”:Bugs:golang-github-containers-commonpackage:UbuntuBug#2040082“errorparsingAppArmorversion”:Bugs:golang-github-containers-commonpackage:Ubuntu......
  • [EFI]技嘉 Z490 VISION G i5-10500 电脑 Hackintosh 黑苹果引导文件
    硬件配置硬件型号驱动情况主板技嘉Z490VISIONGCLPCcontrollerZ490芯片组)处理器英特尔[email protected]六核已驱动内存16GB(威到DDR42655MHz8GBx2〕已驱动硬盘SSDSC2BB150G7R(150GB/国态硬盘)已驱动显卡AMDRadeonRX580(华硕)已驱动声卡瑞昱@英特尔HighDefini......
  • C++ insert into tables of pgsql via libpq-fe.h and compile by g++-13
    1.Installlibpq-devsudoaptinstalllibpq-devlocatelibpq-fe.h/usr/include/postgresql/libpq-fe.h 2.createtablet1createtablet1(idbigserialnotnullprimarykey,authorvarchar(40)notnull,commentvarchar(40)notnull,contentvarchar(40)notn......
  • Get environmentally friendly, biodegradable plastics into the market
      NowdigitaltechsempowerplasticpollutioncombatinChina,andournewmaterialcombinedwithnon-enzymichydrolysis,waterdissolutionandbiodegradationprocesseswaspopularized.   WorkingprincipleRecycleplasticsThroughtheintegrationof......
  • [EFI]戴尔Latitude 5310电脑 Hackintosh 黑苹果efi引导文件
    硬件型号驱动情况主板戴尔Latitude5310处理器IntelCorei5-10210U(1.6GHz/L36M)已驱动内存8GB已驱动硬盘三星MZVLW1T0HMLH-000L2(1024GB/固态硬盘)已驱动显卡IntelUHD620已驱动声卡瑞昱AudioALC299@英特尔HighDefinitionAudio控制器已驱动网卡RTL8168HGigabitEt......
  • Adding empty space into SQL Server Reporting Services
    REF:http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1400080&SiteID=1(1)Ratherthaninsertingspaces,canyouadjustthepaddingpropertyonthetextbox?Ifyougoalistohavethefieldindented,thatshouldwork.(2)Anyreasonyoucouldn'......
  • Import SQL into MySQL with a progress meter
    Thereisnicetoolcalledpv#OnUbuntu/Debiansystem$sudoapt-getinstallpv#OnRedhat/CentOS$sudoyuminstallpvthene.g.youcanuseitlikethis.sql|-uxxx-pxxxxdbname$zcatdbpackfile.sql.gz|pv-cNzcat|mysql-uuser-ppassdbnamePlease......
  • "Grain for Green" program: turn sand into green
    "GrainforGreen"program:turnsandintogreenOnespecificmeasurethathasbeeneffectiveintacklingdesertificationisthe"GrainforGreen"program.TheGrainforGreenprogramwaslaunchedin1999andaimedtoconvertfarmlando......
  • [LeetCode] 2149. Rearrange Array Elements by Sign
    Youaregivena0-indexedintegerarraynumsofevenlengthconsistingofanequalnumberofpositiveandnegativeintegers.Youshouldrearrangetheelementsofnumssuchthatthemodifiedarrayfollowsthegivenconditions:Everyconsecutivepairofint......