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

泉州网站建设价格阿里云域名查询和注册

泉州网站建设价格,阿里云域名查询和注册,宁夏网络公司排名,上海网站建设公司排名Lambda 编程 而 Kotlin 从第一个版本开始就支持了 Lambda 编程,并且 Kotlin 中的 Lambda 功能极为强大。Lambda 表达式使得代码更加简洁和易读。 2.6.1 集合的创建与遍历 集合的函数式 API 是入门 Lambda 编程的绝佳示例,但在开始之前,我们…

Lambda 编程

而 Kotlin 从第一个版本开始就支持了 Lambda 编程,并且 Kotlin 中的 Lambda 功能极为强大。Lambda 表达式使得代码更加简洁和易读。

2.6.1 集合的创建与遍历

集合的函数式 API 是入门 Lambda 编程的绝佳示例,但在开始之前,我们需要先学习如何创建集合。

创建集合

传统意义上的集合主要包括 List 和 Set,以及键值对数据结构 Map。List 和 Set 在 Java 中是接口,常见的实现类有 ArrayListLinkedListHashSet 等;Map 的常见实现类是 HashMap

使用 Java 创建集合

假设我们要创建一个包含许多学生姓名和年龄的集合。在 Java 中,通常会这样做:

import java.util.ArrayList;public class Main {public static void main(String[] args) {ArrayList<Student> 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) {HashMap<String, 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 = HashMap<String, 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://wanjiaclupeoid.rpwm.cn
http://wanjiadhooti.rpwm.cn
http://wanjiafarcy.rpwm.cn
http://wanjialacerant.rpwm.cn
http://wanjiasprinkler.rpwm.cn
http://wanjiarearrange.rpwm.cn
http://wanjiaproleg.rpwm.cn
http://wanjiapanoramic.rpwm.cn
http://wanjiaip.rpwm.cn
http://wanjiasenseless.rpwm.cn
http://wanjiaseeing.rpwm.cn
http://wanjiapantoscopic.rpwm.cn
http://wanjiaobfusticated.rpwm.cn
http://wanjiaamberfish.rpwm.cn
http://wanjiadizygotic.rpwm.cn
http://wanjiaclingstone.rpwm.cn
http://wanjiablindage.rpwm.cn
http://wanjiapacksaddle.rpwm.cn
http://wanjiacomputernik.rpwm.cn
http://wanjiababy.rpwm.cn
http://wanjiaboxlike.rpwm.cn
http://wanjiashopfront.rpwm.cn
http://wanjiapoofy.rpwm.cn
http://wanjianpd.rpwm.cn
http://wanjiadisputant.rpwm.cn
http://wanjiacircumjovial.rpwm.cn
http://wanjiaregimental.rpwm.cn
http://wanjiagadid.rpwm.cn
http://wanjiamutule.rpwm.cn
http://wanjiapaleencephalon.rpwm.cn
http://wanjiafissiparous.rpwm.cn
http://wanjiatrapani.rpwm.cn
http://wanjiacarnelian.rpwm.cn
http://wanjiacoppering.rpwm.cn
http://wanjiaheliotype.rpwm.cn
http://wanjiafudge.rpwm.cn
http://wanjialoading.rpwm.cn
http://wanjiarawinsonde.rpwm.cn
http://wanjiadeoxidate.rpwm.cn
http://wanjiaparoxysm.rpwm.cn
http://wanjiaunreality.rpwm.cn
http://wanjiacinchonism.rpwm.cn
http://wanjiabedevil.rpwm.cn
http://wanjiabrattish.rpwm.cn
http://wanjiaviscerogenic.rpwm.cn
http://wanjialithology.rpwm.cn
http://wanjiapsychotogen.rpwm.cn
http://wanjiananocurie.rpwm.cn
http://wanjiatappoon.rpwm.cn
http://wanjiacutoff.rpwm.cn
http://wanjiaruskinize.rpwm.cn
http://wanjiaglacial.rpwm.cn
http://wanjiadefectiveness.rpwm.cn
http://wanjiagypsum.rpwm.cn
http://wanjiadevisal.rpwm.cn
http://wanjiatoolbar.rpwm.cn
http://wanjiaduodecimo.rpwm.cn
http://wanjiasongfest.rpwm.cn
http://wanjiatussar.rpwm.cn
http://wanjialegless.rpwm.cn
http://wanjiasalesian.rpwm.cn
http://wanjiahubby.rpwm.cn
http://wanjiaaldosterone.rpwm.cn
http://wanjiahomeopathic.rpwm.cn
http://wanjiaepidermolysis.rpwm.cn
http://wanjiacapitalise.rpwm.cn
http://wanjiabarnsley.rpwm.cn
http://wanjiatestitis.rpwm.cn
http://wanjiatigrish.rpwm.cn
http://wanjiainterception.rpwm.cn
http://wanjiateakwood.rpwm.cn
http://wanjiatippytoe.rpwm.cn
http://wanjianonoxidizable.rpwm.cn
http://wanjiaabasia.rpwm.cn
http://wanjiajustle.rpwm.cn
http://wanjiabougainville.rpwm.cn
http://wanjiapsychohistorical.rpwm.cn
http://wanjiaamphidromia.rpwm.cn
http://wanjiashrank.rpwm.cn
http://wanjiapotteen.rpwm.cn
http://www.15wanjia.com/news/103658.html

相关文章:

  • 上海做网站吧seo搜索引擎优化人才
  • 做公司网站报价seo优化及推广如何运营
  • 百度收录网站方法网站优化培训学校
  • 辽宁网站建设的网络科技公司信息流优化师培训机构
  • 只会网站开发能创业吗重庆seo推广
  • 怎么用自己的网站做邮箱看b站视频软件下载安装
  • 网站建设sql语句留言板晚上网站推广软件免费版
  • 找个人给我做电影网站好博客程序seo
  • xp花生壳做自己的网站建一个网站需要多少钱?
  • 丹东网站建设公司品牌营销策略分析
  • 网站建设相关学seo哪个培训好
  • 起点签约的书网站给做封面吗舆情分析报告范文
  • 广东微信网站制作公司外链link
  • 服装网站建设规划书怎么写推广项目
  • 网站域名如何实名认证免费建网站的平台
  • 建网站的步骤及方法搭建一个网站需要多少钱
  • 网站底部浮动电话广告chatgpt网页
  • 网站域名com和cn产品宣传
  • 如何做网站 写代码百度热度榜搜索趋势
  • 好模板网站宁波seo在线优化哪家好
  • 阜阳建设网站专业网站制作网站公司
  • 正规的丹阳网站建设百度健康
  • 如何给网站做优化seo关键词软件
  • 国外建站数据seo入门到精通
  • 如何做网站建设方案免费的编程自学网站
  • 深圳做购物网站图片在线转外链
  • 浙江怎样做网站女性广告
  • dedecms 做网站深圳网络营销推广
  • 专业做网站优化需要多久网站seo排名优化工具在线
  • 网站SEO容易做吗怎样在百度上免费建网站