当前位置:网站首页>[fluent] dart data type list set type (define set | initialize | generic usage | add elements after initialization | set generation function | set traversal)
[fluent] dart data type list set type (define set | initialize | generic usage | add elements after initialization | set generation function | set traversal)
2022-07-02 16:37:00 【Programmer community】
List of articles
- I . Define the collection and initialize
- II . Collection generic usage
- III . Set add elements
- IV . Set generation function
- V . A collection of traverse
- VI . Set example code
I . Define the collection and initialize
Define and initialize List aggregate : Define a collection , And initialize the set ;
① Set element data type : Collection element types are generic , Any data type can be accepted ;
② Set element types : If no generics are specified , Collection can hold different types of elements ,
③ give an example : In a collection that does not specify a generic type int , double , String , bool Element of type ;
④ List Set initialization add elements : Use [] Initialize collection elements ;
List list = [1, 1.0, ' character string ' , true];// Use print You can print collections directly // Print set list : [1, 1.0, character string , true]print(" Print set list : $list");II . Collection generic usage
Set generics :
① Specifying generics : If the collection is declared , Generic specified , Then only elements of this generic type can be stored ;
( Appoint int Collection of generic types , Only store int Element of type )
② Assignment limit : Generic different List Set variables cannot be assigned to each other ;
List<int> list_int = [1 , 2, 3];// Print set list1 : [1, true]print(" Print set list_int : $list_int");III . Set add elements
1 . Set elements add : In addition to adding elements during initialization , You can also call the add ( ) and addAll ( ) Additive elements ;
2 . Add a single element : adopt add ( ) Method Add a single element ;
List list1 = [];list1.add(1);list1.add(true);// Print set list1 : [1, true]print(" Print set list1 : $list1");3 . Add multiple elements : adopt addAll ( ) Method Add multiple elements ;
List list = [1, 1.0, ' character string ' , true]; List list2 = [];list2.addAll(list);// Print set list2 : [1, 1.0, character string , true]print(" Print set list2 : $list2");IV . Set generation function
1 . Set generation function : Call collection List Of generate ( ) Method , You can call the generation function to generate the elements generated according to the requirements ;
2 . generate ( ) The function prototype :
① int length Parameters :List Number of collection elements ;
② E generator(int index) Parameters : Callback function for generating elements , among index Is the element index value ;
/** * Generates a list of values. * * Creates a list with [length] positions and fills it with values created by * calling [generator] for each index in the range `0` .. `length - 1` * in increasing order. * * new List<int>.generate(3, (int index) => index * index); // [0, 1, 4] * * The created list is fixed-length unless [growable] is true. */ factory List.generate(int length, E generator(int index), {
bool growable = true}) {
List<E> result; if (growable) {
result = <E>[]..length = length; } else {
result = List<E>(length); } for (int i = 0; i < length; i++) {
result[i] = generator(i); } return result; }3 . Code example :
// The generating function of the set // int length Parameters : The length of the set // E generator(int index) : Callback function of collection , Call this function to get the index The element of location List list_generate = List.generate(3, ( index ) => index * 3);// Print set list_generate : [0, 3, 6]print(" Print set list_generate : $list_generate");V . A collection of traverse
1 . adopt With cyclic conditions Ordinary for Loop traversal :
// 1 . The way 1 : Access the collection by subscript for(int i = 0; i < list_generate.length; i ++){
print(list_generate[i]);}2 . adopt var obj in list_generate Styling for Loop traversal :
// 2 . The way 2 : adopt var obj in list_generate Get the elements in the collection for( var obj in list_generate ){
print(obj);}3 . adopt For Each Loop traversal :
// 3 . The way 3 : For Each loop list_generate.forEach( ( element ){
print(element);} );VI . Set example code
1 . Sample code :
import 'package:flutter/material.dart';class DartType_List extends StatefulWidget {
@override _DartType_ListState createState() => _DartType_ListState();}class _DartType_ListState extends State<DartType_List> {
@override Widget build(BuildContext context) {
listDemo(); return Container(child: Text('List data type ')); } /** * List Example set */ listDemo(){
// I . Define a collection // Define a collection , And initialize the set // Set element data type : The collection element type is generic , Any data type can be accepted // Set element types : If no generics are specified , Collection can hold different types of elements // For example, it is stored in a collection without specifying a generic type int , double , String , bool Element of type // Initialize add element : Use [] Initialize collection elements List list = [1, 1.0, ' character string ' , true]; // Use print You can print collections directly // Print set list : [1, 1.0, character string , true] print(" Print set list : $list"); // II . Collection generic usage // If the collection is declared , Generic specified , Then only elements of this generic type can be stored // Such as : Appoint int Collection of generic types , Only store int Element of type // Generic different List Set variables cannot be assigned to each other // Don't put the above list Set is assigned to The list_int List<int> list_int = [1 , 2, 3]; // Print set list1 : [1, true] print(" Print set list_int : $list_int"); // III . Add elements after initialization // In addition to adding elements during initialization // You can also call the add ( ) and addAll ( ) Additive elements // adopt add ( ) Method Add a single element List list1 = []; list1.add(1); list1.add(true); // Print set list1 : [1, true] print(" Print set list1 : $list1"); // adopt addAll ( ) Method Add multiple elements List list2 = []; list2.addAll(list); // Print set list2 : [1, 1.0, character string , true] print(" Print set list2 : $list2"); // IV . The generating function of the set // The generating function of the set // int length Parameters : The length of the set // E generator(int index) : Callback function of collection , Call this function to get the index The element of location List list_generate = List.generate(3, ( index ) => index * 3); // Print set list_generate : [0, 3, 6] print(" Print set list_generate : $list_generate"); // V . A collection of traverse // 1 . The way 1 : Access the collection by subscript for(int i = 0; i < list_generate.length; i ++){
print(list_generate[i]); } // 2 . The way 2 : adopt var obj in list_generate Get the elements in the collection for( var obj in list_generate ){
print(obj); } // 3 . The way 3 : For Each loop list_generate.forEach( ( element ){
print(element); } ); }}2 . Execution results :
Print set list : [1, 1.0, character string , true] Print set list_int : [1, 2, 3] Print set list1 : [1, true] Print set list2 : [1, 1.0, character string , true] Print set list_generate : [0, 3, 6]036036036边栏推荐
- Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
- Yyds dry inventory executor package (parameter processing function)
- Source code look me
- 台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...
- 数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙
- Multi task prompt learning: how to train a large language model?
- Everyone Xinfu builds: a one-stop intelligent business credit service platform
- MySQL min() finds the minimum value under certain conditions, and there are multiple results
- Maui学习之路(三)--Winui3深入探讨
- JS learning notes - variables
猜你喜欢

What if the win11 app store cannot load the page? Win11 store cannot load page

中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖

La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter

Download blender on Alibaba cloud image station

2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time

TCP拥塞控制详解 | 2. 背景

总结|机器视觉中三大坐标系及其相互关系

Practice of traffic recording and playback in vivo

Data security industry series Salon (III) | data security industry standard system construction theme Salon

Summary of monthly report | list of major events of moonbeam in June
随机推荐
Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share
外企高管、连续创业者、瑜伽和滑雪高手,持续迭代重构的程序人生
Route service grid traffic through two-level gateway design
流批一体在京东的探索与实践
TCP拥塞控制详解 | 2. 背景
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
How to choose the right kubernetes storage plug-in? (09)
分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
Today in history: Alipay launched barcode payment; The father of time-sharing system was born; The first TV advertisement in the world
Classic quotations
unity Hub 登录框变得很窄 无法登录
The light of ideal never dies
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 releases | geek headlines
LeetCode 6. Z 字形变换 (N字形变换)
SQLServer查询哪些索引利用率低
PCL point cloud image transformation
Maui学习之路(三)--Winui3深入探讨