首页 > 其他分享 >无涯教程-F# - 可变列表(Mutable List)

无涯教程-F# - 可变列表(Mutable List)

时间:2023-11-28 23:32:11浏览次数:37  
标签:index Narnia List 无涯 Add booksList Mutable printfn Rebecca

List <'T>类表示可以通过索引访问的对象的类型列表,它与数组相似,因为它可以由索引访问,但是,与数组不同,可以调整列表的大小。

创建可变列表

使用 new 关键字并调用列表的构造函数来创建列表。以下示例演示了这一点-

(* Creating a List *)
open System.Collections.Generic

let booksList = new List<string>()
booksList.Add("Gone with the Wind")
booksList.Add("Atlas Shrugged")
booksList.Add("Fountainhead")
booksList.Add("Thornbirds")
booksList.Add("Rebecca")
booksList.Add("Narnia")

booksList |> Seq.iteri (fun index item -> printfn "%i: %s" index booksList.[index])

编译并执行程序时,将产生以下输出-

0: Gone with the Wind
1: Atlas Shrugged
2: Fountainhead
3: Thornbirds
4: Rebecca
5: Narnia

可变列表示例

(* Creating a List *)
open System.Collections.Generic

let booksList = new List<string>()
booksList.Add("Gone with the Wind")
booksList.Add("Atlas Shrugged")
booksList.Add("Fountainhead")
booksList.Add("Thornbirds")
booksList.Add("Rebecca")
booksList.Add("Narnia")

printfn"Total %d books" booksList.Count
booksList |> Seq.iteri (fun index item -> printfn "%i: %s" index booksList.[index])
booksList.Insert(2, "Roots")

printfn("after inserting at index 2")
printfn"Total %d books" booksList.Count

booksList |> Seq.iteri (fun index item -> printfn "%i: %s" index booksList.[index])
booksList.RemoveAt(3)

printfn("after removing from index 3")
printfn"Total %d books" booksList.Count

booksList |> Seq.iteri (fun index item -> printfn "%i: %s" index booksList.[index])

编译并执行程序时,将产生以下输出-

Total 6 books
0: Gone with the Wind
1: Atlas Shrugged
2: Fountainhead
3: Thornbirds
4: Rebecca
5: Narnia
after inserting at index 2
Total 7 books
0: Gone with the Wind
1: Atlas Shrugged
2: Roots
3: Fountainhead
4: Thornbirds
5: Rebecca
6: Narnia
after removing from index 3
Total 6 books
0: Gone with the Wind
1: Atlas Shrugged
2: Roots
3: Thornbirds
4: Rebecca
5: Narnia

参考链接

https://www.learnfk.com/fsharp/fsharp-mutable-lists.html

标签:index,Narnia,List,无涯,Add,booksList,Mutable,printfn,Rebecca
From: https://blog.51cto.com/u_14033984/8605419

相关文章

  • 无涯教程-F# - 映射(Maps)
    在F#中,字典(Maps)是一种特殊的集合,将值(value)与键(key)相关联。创建字典通过使用Map.empty创建空Map并使用添加功能添加项目来创建Map。以下示例演示了这一点-(*CreateanemptyMap*)letstudents=Map.empty.(*CreatinganemptyMap*)Add("ZaraAli","1......
  • Java集合框架之:ArrayList的常见方法使用
    ✨前言✨  Java本文主要介绍JavaArrayList的使用方法教程及示例代码......
  • 【python入门之基本数据类型的学习】---基本数据类型(list、str)【二】
    【三】字符串类型(str)【1】作用字符串类型(str)用于表示文本信息,是一种非常重要的数据类型,用于处理文字、字符等信息【2】定义(1)定义方式字符串可以使用单引号、双引号或三引号进行定义#定义方式1:name_1='Jack'#(当左边有赋值符号和遍历名的时候,它就是字符串)#定......
  • 无涯教程-F# - 列表(Lists)
    在F#中,列表(Lists)是相同类型的元素的有序、不可变的组合,它在某种程度上等效于链表数据结构。F#模块Microsoft.FSharp.Collections.List具有列表上的常用操作,但是,F#会自动导入此模块,并使每个F#应用程序都可以访问它。List初始化以下是创建列表的各种方法-使用List字面量。......
  • 无涯教程-F# - 记录(Records)
    记录(Records)类似于元组,但是它包含命名字段。例如,typewebsite={title:string;url:string}定义记录使用type关键字将记录定义为类型,并将记录的字段定义为以分号分隔的列表。定义记录的语法是-typerecordName={[fieldName:dataType]+}创建......
  • List去除重复数据的推荐方式 (*2)
    List去除重复数据的推荐方式(*2)推荐理由:保证顺序的同时去掉重复元素。综合考量:在不考虑顺序的情况下使用HashSet和循环可取,一定程度上提升性能。 方式1:使用java8新特性stream进行List去重(强烈建议,简便实用)要从arraylist中删除重复项,我们也可以使用java8streamapi。......
  • python中列表(list)拼接的三种方法
    你可以使用Python中的列表拼接操作来合并两个或多个列表。Python提供了几种方式来实现列表的拼接,包括使用+运算符、extend()方法和列表解析。以下是这些方法的示例:1.使用+运算符:list1=[1,2,3]list2=[4,5,6]concatenated_list=list1+list2print(concate......
  • 无涯教程-F# - 数据类型
    F#中的数据类型可以分类如下-整数类型浮点类型文本类型其他类型整体数据类型下表提供了F#的整数数据类型,这些基本上是整数数据类型。F#TypeSizeRangeExampleRemarkssbyte1byte-128to12742y-11y8-bitsignedintegerbyte1byte0to25542uy200uy8-......
  • 集合框架(三)ArrayList的常见使用
    优点:有序,可以重复,查询快。缺点:增删慢。List接口常用方法:1、add(Objectelement):向列表的尾部添加指定的元素。2、size():返回列表中的元素个数。3、get(intindex):返回列表中指定位置的元素,index从0开始。4、add(intindex,Objectelement):在列表的指定位置插入指定元......
  • list求差集的方法汇总(两个不同元素的List集合)
    一个全部用户集合List<UserInfo>allUser,一个部分用户集合List<UserInfo>commentUser,根据UserInfo中的UserID求差集,从allUser中得到剩下的一部分用户,通过stream流和lamda表达式实现publicList<UserInfo>getNotComment(List<UserInfo>allUser,List<UserInfo>commentU......