Oktatás * Programozás 2 * Szkriptnyelvek * levelezősök Félévek Linkek * kalendárium |
Scala /
Array and ArrayBuffer
ArrayAn Array is mutable and has fixed size. scala> val nums = new Array[Int](5) // an array of 5 integers nums: Array[Int] = Array(0, 0, 0, 0, 0) // initialized with 0s scala> val words = Array("hello", "world") // no "new" when giving initial values words: Array[String] = Array(hello, world) scala> words(0) = "Hello" // use () instead of [] to access an element scala> words res128: Array[String] = Array(Hello, world) ArrayBufferMutable, its size can vary. import scala.collection.mutable.ArrayBuffer // available from here scala> val b = ArrayBuffer[Int]() // option 1 to create an empty arraybuffer b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer() scala> val b = new ArrayBuffer[Int] // option 2 b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer() scala> val b = ArrayBuffer(2, 5, 8) // initialize with some values b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2, 5, 8) scala> b += 10 // add an element to its end res129: b.type = ArrayBuffer(2, 5, 8, 10) scala> b -= 5 // remove a given element res130: b.type = ArrayBuffer(2, 8, 10) scala> b += (4, 7) // add multiple elements res131: b.type = ArrayBuffer(2, 8, 10, 4, 7) scala> b ++= Array(15, 16) // ++= operator: append a collection res132: b.type = ArrayBuffer(2, 8, 10, 4, 7, 15, 16) Array → ArrayBuffer / ArrayBuffer → Arrayscala> Array(3, 6).toBuffer res133: scala.collection.mutable.Buffer[Int] = ArrayBuffer(3, 6) scala> ArrayBuffer(4, 8).toArray res134: Array[Int] = Array(4, 8) |
Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |