Last night, I listened to the first lesson of Liaoliang's Spark 3000 disciple series, took the Scala foundation and gave Arraybuffer a new understanding:
The array itself cannot be modified after it is created
Arraybuffer can be modified
Import Scala.collection.mutable.ArrayBuffer
Val arrb = Arraybuffer[int] ()
ARRB + = 10
ARRB + = (11,12,13,5)
ARRB ++= Array (1,2,3,5)
Arrb.trimend (3) Truncation of 3 from the tail
Arrb.trimstart (3) truncated from head
Arrb.insert (5,100) Fifth position add number 100
Arrb.insert (7,9,2) Seventh position add a bunch of numbers
Arrb.remove (10) Delete the 10th element, the return value is the deleted value
Arrb.remove (10,3) Delete the first 3 elements of the 10th
Val arr = Arrb.toarray
ArrB2 = Arr.tobuffer
for (Elem <-arr) println (elem)
There is also a post-lesson assignment: Remove all negative numbers after the first negative number in an array
Here's my answer:
Import Scala.util.control.breaks._
Object Arrayrm {
def main (args:array[string]): Unit = {
Val arr = Rmfu (Array (1,-2,3,-4,-5,6,-7,8))
for (i <-arr) println (i)
}
def RMFU (Arr:array[int]) = {
Val ArrayBuffer = Arr.tobuffer
var k = 0
breakable {
for (i <-ArrayBuffer) {
if (i<0) {
K
Break
}
K + = 1
}
}
println ("k=" + k)
for (I <-(0 until arraybuffer.length). Reverse) {
if (i!=k && arrayBuffer (i) <0) arraybuffer.remove (i)
}
Arraybuffer.toarray
}
}
Spark 3000 Disciples First lesson essays