当前位置:网站首页>[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边栏推荐
- 学生选课系统(山东农业大学课程设计)
- Yyds dry inventory method of deleting expired documents in batch
- 分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
- SQLServer查询哪些索引利用率低
- Sqlserver queries which indexes are underutilized
- 总结|机器视觉中三大坐标系及其相互关系
- What if the win11 app store cannot load the page? Win11 store cannot load page
- How to use stustr function in Oracle view
- Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
- 关于mysql安装的一些问题
猜你喜欢

End time processing method of wechat v3native payment settings

Maui学习之路(三)--Winui3深入探讨

Compress words (kmp/ string hash, double hash)

unity Hub 登錄框變得很窄 無法登錄

Multi task prompt learning: how to train a large language model?

SQL solves the problem of continuous login deformation holiday filtering
![[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology](/img/5c/5f835c286548907f3f09ecb66b2068.jpg)
[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology

数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙
![[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](/img/51/f9c1eed37794db8c8d0eefd60b9e3d.jpg)
[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

去除router-link中的下划线
随机推荐
请问怎么在oracle视图中使用stustr函数
LeetCode 4. 寻找两个正序数组的中位数(hard)
How to choose the right kubernetes storage plug-in? (09)
Foreign enterprise executives, continuous entrepreneurs, yoga and skiing masters, and a program life of continuous iteration and reconstruction
PyC file decompile
理想之光不灭
Leetcode -- number of palindromes
Summary of multithreading and thread synchronization knowledge
(practice C language every day) the sum of the nearest three numbers
学生选课系统(山东农业大学课程设计)
Written by unity Jason
LeetCode 5. 最长回文子串
Cloud native cicd framework: Tekton
关于mysql安装的一些问题
ROW_NUMBER()、RANK()、DENSE_RANK区别
Library management system (Shandong Agricultural University Curriculum Design)
Maui学习之路(三)--Winui3深入探讨
[fluent] dart language (DART language features | JIT instant compilation | AOT static compilation)
Download blender on Alibaba cloud image station
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation