Spark 3000 Disciples First lesson essays

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.