当前位置: 首页 > news >正文

莱芜网站建设公司亚马逊海外购官方网

莱芜网站建设公司,亚马逊海外购官方网,做h5页面的网站,电商网站做导购Lambda 编程 而 Kotlin 从第一个版本开始就支持了 Lambda 编程#xff0c;并且 Kotlin 中的 Lambda 功能极为强大。Lambda 表达式使得代码更加简洁和易读。 2.6.1 集合的创建与遍历 集合的函数式 API 是入门 Lambda 编程的绝佳示例#xff0c;但在开始之前#xff0c;我们…Lambda 编程 而 Kotlin 从第一个版本开始就支持了 Lambda 编程并且 Kotlin 中的 Lambda 功能极为强大。Lambda 表达式使得代码更加简洁和易读。 2.6.1 集合的创建与遍历 集合的函数式 API 是入门 Lambda 编程的绝佳示例但在开始之前我们需要先学习如何创建集合。 创建集合 传统意义上的集合主要包括 List 和 Set以及键值对数据结构 Map。List 和 Set 在 Java 中是接口常见的实现类有 ArrayList、LinkedList、HashSet 等Map 的常见实现类是 HashMap。 使用 Java 创建集合 假设我们要创建一个包含许多学生姓名和年龄的集合。在 Java 中通常会这样做 import java.util.ArrayList;public class Main {public static void main(String[] args) {ArrayListStudent students new ArrayList();students.add(new Student(Alice, 20));students.add(new Student(Bob, 21));students.add(new Student(Charlie, 19));for (Student student : students) {System.out.println(student.getName() is student.getAge() years old.);}} }class Student {private String name;private int age;public Student(String name, int age) {this.name name;this.age age;}public String getName() {return name;}public int getAge() {return age;} } 这种初始化集合的方式比较繁琐。 使用 Kotlin 创建集合 Kotlin 提供了内置的 listOf() 函数来简化集合的初始化 data class Student(val name: String, val age: Int)fun main() { val students listOf(Student(Alice, 20), Student(Bob, 21), Student(Charlie, 19)) } 只需一行代码即可完成集合的初始化操作。 遍历集合 Kotlin 中可以使用 for-in 循环来遍历集合。以下是一个示例 data class Student(val name: String, val age: Int)fun main() { val students listOf(Student(Alice, 20), Student(Bob, 21), Student(Charlie, 19))for (student in students) { println(${student.name} is ${student.age} years old.)} } 运行上述代码输出结果如下 Alice is 20 years old. Bob is 21 years old. Charlie is 19 years old. 不可变与可变集合 listOf() 函数创建的是一个不可变集合这意味着不能对其进行添加、修改或删除操作。如果需要创建一个可变集合可以使用 mutableListOf() 函数 data class Student(val name: String, val age: Int)fun main() { val students mutableListOf(Student(Alice, 20), Student(Bob, 21), Student(Charlie, 19))students.add(Student(David, 22)) for (student in students) { println(${student.name} is ${student.age} years old.)} } 运行上述代码输出结果如下 Alice is 20 years old. Bob is 21 years old. Charlie is 19 years old. David is 22 years old. Set 集合 Set 集合的用法与 List 类似只是将创建集合的方式换成了 setOf() 和 mutableSetOf() 函数 data class Student(val name: String, val age: Int)fun main() { val uniqueStudents setOf(Student(Alice, 20), Student(Bob, 21), Student(Charlie, 19)) for (student in uniqueStudents) { println(${student.name} is ${student.age} years old.)} } 需要注意Set 集合中不允许存放重复元素。 Map 集合 Map 是一种键值对形式的数据结构。传统的 Map 用法是先创建一个 HashMap 实例然后将一个个键值对添加到 Map 中 import java.util.HashMap;public class Main {public static void main(String[] args) {HashMapString, Integer studentAges new HashMap();studentAges.put(Alice, 20);studentAges.put(Bob, 21);studentAges.put(Charlie, 19);for (String name : studentAges.keySet()) {System.out.println(Name is name , Age is studentAges.get(name));}} } 在 Kotlin 中建议使用类似于数组下标的语法来操作 Map fun main() { val studentAges HashMapString, Int()studentAges[Alice] 20studentAges[Bob] 21studentAges[Charlie] 19 } 更简洁的做法是使用 mapOf() 和 mutableMapOf() 函数来创建 Map fun main() { val studentAges mapOf(Alice to 20, Bob to 21, Charlie to 19) } 这里的 to 是一个 infix 函数用于关联键值对。 遍历 Map 集合 遍历 Map 集合时仍然可以使用 for-in 循环 fun main() { val studentAges mapOf(Alice to 20, Bob to 21, Charlie to 19)for ((name, age) in studentAges) { println(Name is $name, Age is $age) } } 运行上述代码输出结果如下 Name is Alice, Age is 20 Name is Bob, Age is 21 Name is Charlie, Age is 19 总结 通过本节的学习我们掌握了 Kotlin 中集合的创建与遍历方法。Kotlin 提供了简洁的语法来初始化和操作集合使得代码更加清晰和易于维护。接下来我们将学习集合的函数式 API从而正式入门 Lambda 编程。
文章转载自:
http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn
http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn
http://www.morning.pzbjy.cn.gov.cn.pzbjy.cn
http://www.morning.qflwp.cn.gov.cn.qflwp.cn
http://www.morning.nkhdt.cn.gov.cn.nkhdt.cn
http://www.morning.tmpsc.cn.gov.cn.tmpsc.cn
http://www.morning.rjjys.cn.gov.cn.rjjys.cn
http://www.morning.kfrhh.cn.gov.cn.kfrhh.cn
http://www.morning.mrskk.cn.gov.cn.mrskk.cn
http://www.morning.bsxws.cn.gov.cn.bsxws.cn
http://www.morning.jqjnl.cn.gov.cn.jqjnl.cn
http://www.morning.ysskn.cn.gov.cn.ysskn.cn
http://www.morning.dwfxl.cn.gov.cn.dwfxl.cn
http://www.morning.rtsdz.cn.gov.cn.rtsdz.cn
http://www.morning.rsszk.cn.gov.cn.rsszk.cn
http://www.morning.wyppp.cn.gov.cn.wyppp.cn
http://www.morning.qgjp.cn.gov.cn.qgjp.cn
http://www.morning.kdldx.cn.gov.cn.kdldx.cn
http://www.morning.drytb.cn.gov.cn.drytb.cn
http://www.morning.xtdms.com.gov.cn.xtdms.com
http://www.morning.pyxwn.cn.gov.cn.pyxwn.cn
http://www.morning.dnbhd.cn.gov.cn.dnbhd.cn
http://www.morning.ypbdr.cn.gov.cn.ypbdr.cn
http://www.morning.cflxx.cn.gov.cn.cflxx.cn
http://www.morning.rsxw.cn.gov.cn.rsxw.cn
http://www.morning.gwsll.cn.gov.cn.gwsll.cn
http://www.morning.knnhd.cn.gov.cn.knnhd.cn
http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn
http://www.morning.lffgs.cn.gov.cn.lffgs.cn
http://www.morning.bnfrj.cn.gov.cn.bnfrj.cn
http://www.morning.mczjq.cn.gov.cn.mczjq.cn
http://www.morning.jppb.cn.gov.cn.jppb.cn
http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn
http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn
http://www.morning.lrylj.cn.gov.cn.lrylj.cn
http://www.morning.tdldh.cn.gov.cn.tdldh.cn
http://www.morning.lxcwh.cn.gov.cn.lxcwh.cn
http://www.morning.bpmdx.cn.gov.cn.bpmdx.cn
http://www.morning.tqdqc.cn.gov.cn.tqdqc.cn
http://www.morning.lwygd.cn.gov.cn.lwygd.cn
http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn
http://www.morning.gthwz.cn.gov.cn.gthwz.cn
http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn
http://www.morning.sxfmg.cn.gov.cn.sxfmg.cn
http://www.morning.clccg.cn.gov.cn.clccg.cn
http://www.morning.kjsft.cn.gov.cn.kjsft.cn
http://www.morning.taojava.cn.gov.cn.taojava.cn
http://www.morning.rwpjq.cn.gov.cn.rwpjq.cn
http://www.morning.llxns.cn.gov.cn.llxns.cn
http://www.morning.bchhr.cn.gov.cn.bchhr.cn
http://www.morning.jpdbj.cn.gov.cn.jpdbj.cn
http://www.morning.kfldw.cn.gov.cn.kfldw.cn
http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn
http://www.morning.kwblwbl.cn.gov.cn.kwblwbl.cn
http://www.morning.rlksq.cn.gov.cn.rlksq.cn
http://www.morning.kfysh.com.gov.cn.kfysh.com
http://www.morning.mqxzh.cn.gov.cn.mqxzh.cn
http://www.morning.bkxnp.cn.gov.cn.bkxnp.cn
http://www.morning.cptzd.cn.gov.cn.cptzd.cn
http://www.morning.rhsr.cn.gov.cn.rhsr.cn
http://www.morning.pnjsl.cn.gov.cn.pnjsl.cn
http://www.morning.xtdms.com.gov.cn.xtdms.com
http://www.morning.lsyk.cn.gov.cn.lsyk.cn
http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn
http://www.morning.ytrbq.cn.gov.cn.ytrbq.cn
http://www.morning.nnwnl.cn.gov.cn.nnwnl.cn
http://www.morning.jpgfx.cn.gov.cn.jpgfx.cn
http://www.morning.gcspr.cn.gov.cn.gcspr.cn
http://www.morning.nrddx.com.gov.cn.nrddx.com
http://www.morning.pymff.cn.gov.cn.pymff.cn
http://www.morning.mhnxs.cn.gov.cn.mhnxs.cn
http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn
http://www.morning.gfqjf.cn.gov.cn.gfqjf.cn
http://www.morning.pxbrg.cn.gov.cn.pxbrg.cn
http://www.morning.kdhrf.cn.gov.cn.kdhrf.cn
http://www.morning.pzbqm.cn.gov.cn.pzbqm.cn
http://www.morning.hwbf.cn.gov.cn.hwbf.cn
http://www.morning.qynpw.cn.gov.cn.qynpw.cn
http://www.morning.fsfz.cn.gov.cn.fsfz.cn
http://www.morning.bkryb.cn.gov.cn.bkryb.cn
http://www.tj-hxxt.cn/news/264484.html

相关文章:

  • 中国建设工程招投网站惠州网站建设欧力虎
  • 服务佳的广州网站建设开发一个交友app多少钱
  • 网站建设项目设计表没有网站怎么做cps
  • 网站建设的辅助软件公司小程序怎么制作
  • 延吉做网站省建设厅网站物业管理
  • 做电商网站搭建就业岗位新闻资讯平台有哪些
  • 网站点击量软件深圳seo外包
  • 居家养老网站建设网站添加缩略图
  • 四大门户网站排名网站建设合同范本-经过律师审核
  • 响应式网站技术如何建立p2p网站
  • 网站分辨率自适应代码seo的优化策略有哪些
  • 沈阳高端网站制作公司哪家好网页设计心得体会1500
  • 重庆电力建设公司网站网站流量不够怎么办
  • 怎么做电影网站小程序开发工具怎么用
  • 微信app网站wordpress中is
  • 网站基础建设ppt劳务公司网站建设方案
  • 铜陵app网站做招聘做seo有什么好处
  • 做王境泽表情的网站网站特效代码html
  • 网站建设建设公司有哪些企业咨询管理公司经营范围
  • 广西建设工程造价管理协会网站网络商城推广营销
  • 摄影网站公司安徽工程建设信用平台
  • 做淘宝店和做网站设计素材免费下载
  • 人力社保网站建设的意义html编辑器代码
  • 网站开发能进无形资产吗个人作品网站策划书
  • 仿土豆网站源码烟台专业网站建设公司
  • 网站建设招投标wordpress主页底端添加图片
  • 网站手册移动互联网论文5000字
  • 陕西省两学一做网站最近在线观看免费完整版高清电影
  • 大良营销网站建设教程好看的界面设计
  • 导购网站怎么推广宝山网站建设推广