package com.bigdata import scala.collection.mutable.ArrayBuffer object ArrayO { def main(args: Array[String]): Unit = { val arrayBuffer = ArrayBuffer[Int]() //默认情况下都是在ArrayBuffer末尾增加元素 arrayBuffer += 1 arrayBuffer += (4,5,6,7,8,9,10) arrayBuffer ++= Array(1,2,3) println(arrayBuffer) //对数组元素访问取下标 println(arrayBuffer(0)) //添加元素 arrayBuffer.append(101) println(arrayBuffer) //插入元素 arrayBuffer.insert(arrayBuffer.length - 1, 100,1000) println(arrayBuffer) //移除元素 arrayBuffer.remove(arrayBuffer.length -2) println(arrayBuffer) //当需要多线程并发操作的时候,把ArrayBuffer转换成为Array就非常重要。 val arr: Array[Int] = arrayBuffer.toArray for(item <- arrayBuffer) println(item) } }