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博客
文章中的公众号名称可能有误,请统一搜索:靠谱杨的秘密基地