zoukankan      html  css  js  c++  java
  • Dart语言学习(六) Dart 列表List数组

    List里面常用的属性和方法:
    
        常用属性:
            length          长度
            reversed        翻转
            isEmpty         是否为空
            isNotEmpty      是否不为空
        常用方法:  
            add         增加
            addAll      拼接数组
            indexOf     查找  传入具体值
            remove      删除  传入具体值
            removeAt    删除  传入索引值
            fillRange   修改   
            insert(index,value);            指定位置插入    
            insertAll(index,list)           指定位置插入List
            toList()    其他类型转换成List  
            join()      List转换成字符串
            split()     字符串转化成List
            forEach   
            map
            where
            any
            every

    一、创建list

    创建List : var list = [1,2,3,"Dart",true];

    创建不可变List : var list = const [1,2,3,"Dart",true];

    构造创建:var list3 = new List();

    二、常用操作

    [],length,add(),insert(),remove(),clear()
    indexOf(),lastIndexOf()
    排序sort(),子数组 sublist()
    打乱shuffle(),转为Map: asMap(),遍历forEach()
    实例代码如下:
      //创建list
      var list1 = [1,2,3,"Dart",true];
      print(list1);
      //下标索引打印对于元素
      print(list1[2]);
      list1[1] = "Hello";
      print(list1);
    
      //创建不可变元素
      var list2 = const [1,2,3];
      //  list2[0] = 5;  The value of the local variable 'list2' isn't used.
    
      //构造方法创建
      var list3 = new List();
      print(list3);
    
      var list = ["hello","dart"];
      print(list.length);
      list.add("New");
      print(list);
    
      //插入在index为1的位置
      list.insert(1, "Java");
      print(list);
    
      //移除元素
      list.remove("Java");
      print(list);
    
      //元素对应的位置
      print(list.indexOf("dart1"));
      //排序
      list.sort();
      print(list);
      //截取从1开始
      print(list.sublist(1));
    
      list.forEach(print);
    
      list.clear();
      print(list);
    输出:
    [1, 2, 3, Dart, true]
    3
    [1, Hello, 3, Dart, true]
    []
    2
    [hello, dart, New]
    [hello, Java, dart, New]
    [hello, dart, New]
    -1
    [New, dart, hello]
    [dart, hello]
    New
    dart
    hello
    []
    Dart学习系列文章:https://www.cnblogs.com/jukaiit/category/1636484.html
  • 相关阅读:
    SP笔记:交叉实现七行并成一行
    HTML tag 学习
    操作哈希表
    Efficient bipedal robots based on passivedynamic walkers
    Pushing People Around
    ZEROMOMENT PONTTHIRTY FIVE YEARS OF ITS LIFE

    Active Learning for RealTime Motion Controllers
    Accelerometerbased User Interfaces for the Control of a Physically Simulated Character
    Dynamic Response for Motion Capture Animation
  • 原文地址:https://www.cnblogs.com/jukaiit/p/12236498.html
Copyright © 2011-2022 走看看