Scala 可变列表ListBuffer
1 package chapter07
2
3 import scala.collection.mutable.ListBuffer
4
5 object Test05_ListBuffer {
6 def main(args: Array[String]): Unit = {
7 // 1. 创建可变列表
8 val list1: ListBuffer[Int] = new ListBuffer[Int]()
9 val list2 = ListBuffer(12, 53, 75) //推荐直接使用伴生对象创建
10
11 println(list1)
12 println(list2)
13
14 println("==============")
15
16 // 2. 添加元素
17 list1.append(15, 62)
18 list2.prepend(20)
19
20 list1.insert(1, 19, 22)
21
22 println(list1)
23 println(list2)
24
25 println("==============")
26
27 31 +=: 96 +=: list1 += 25 += 11
28 println(list1)
29
30 println("==============")
31 // 3. 合并list
32 val list3 = list1 ++ list2
33 println(list1)
34 println(list2)
35
36 println("==============")
37
38 list1 ++=: list2
39 println(list1)
40 println(list2)
41
42 println("==============")
43
44 // 4. 修改元素
45 list2(3) = 30
46 list2.update(0, 89)
47 println(list2)
48
49 // 5. 删除元素
50 list2.remove(2)
51 list2 -= 25
52 println(list2)
53 }
54 }
好看请赞,养成习惯:) 本文来自博客园,作者:靠谱杨, 转载请注明原文链接:https://www.cnblogs.com/rainbow-1/p/15821700.html
欢迎来我的51CTO博客主页踩一踩 我的51CTO博客
文章中的公众号名称可能有误,请统一搜索:靠谱杨的秘密基地
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2021-01-19 Android开发入门