当前位置:网站首页>[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边栏推荐
- Library management system (Shandong Agricultural University Curriculum Design)
- unity Hub 登錄框變得很窄 無法登錄
- 理想之光不灭
- 月报总结|Moonbeam6月份大事一览
- Write your own CPU Chapter 11 - learning notes
- Win11应用商店无法加载页面怎么办?Win11商店无法加载页面
- Which software is good for machine vision?
- 外企高管、连续创业者、瑜伽和滑雪高手,持续迭代重构的程序人生
- 头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》
- 绝对真理和相对真理思考
猜你喜欢

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

Mysql database mysqldump why there is no statement to create a database

JS learning notes - process control

HMS core machine learning service helps zaful users to shop conveniently

数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙

Does bone conduction earphone have external sound? Advantages of bone conduction earphones

How to use stustr function in Oracle view

原神2.6服务端下载以及搭建安装教程

Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus

Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)
随机推荐
Mobile web development learning notes - Layout
Yyds dry inventory KVM new inventory to expand space for home
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
LeetCode 4. 寻找两个正序数组的中位数(hard)
Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)
Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
Library management system (Shandong Agricultural University Curriculum Design)
(practice C language every day) the sum of the nearest three numbers
unity Hub 登錄框變得很窄 無法登錄
sql解决连续登录问题变形-节假日过滤
月报总结|Moonbeam6月份大事一览
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
Everyone Xinfu builds: a one-stop intelligent business credit service platform
Recalling the college entrance examination and becoming a programmer, do you regret it?
总结|机器视觉中三大坐标系及其相互关系
学生选课系统(山东农业大学课程设计)
Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share