当前位置:网站首页>[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
边栏推荐
- SSM整合-异常处理器及项目异常处理方案
- Take you ten days to easily complete the go micro service series (I)
- Routing mode: hash and history mode
- JS learning notes - first acquaintance
- Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)
- Classic quotations
- AWS virtual machine expansion
- Cloud native cicd framework: Tekton
- End time processing method of wechat v3native payment settings
- 电脑设备打印机驱动安装失败如何解决
猜你喜欢
Which software is good for machine vision?
Effectively use keywords to increase Amazon sales
Everyone Xinfu builds: a one-stop intelligent business credit service platform
mysql min() 求某条件下最小的值出现多个结果
2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time
Yyds dry goods inventory # look up at the sky | talk about the way and principle of capturing packets on the mobile terminal and how to prevent mitm
数学分析_笔记_第6章:一元函数的Riemann积分
Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
Understand the key technology of AGV -- the difference between laser slam and visual slam
渗透工具-内网权限维持-Cobalt strike
随机推荐
Thinking about absolute truth and relative truth
总结|机器视觉中三大坐标系及其相互关系
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
PyC file decompile
Unity Json 编写
618 deep resumption: Haier Zhijia's winning methodology
Maui学习之路(三)--Winui3深入探讨
虚假的暑假
Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
Aike AI frontier promotion (2.15)
数学分析_笔记_第6章:一元函数的Riemann积分
Yyds dry inventory company stipulates that all interfaces use post requests. Why?
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)
Memory alignment of structure
PCL point cloud image transformation
Which software is good for machine vision?
Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖