Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

* Scripting Languages
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* aktuális (2023/24/2)
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Array and ArrayBuffer

#############################

Array

An 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)

ArrayBuffer

Mutable, 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 → Array

scala> Array(3, 6).toBuffer
res133: scala.collection.mutable.Buffer[Int] = ArrayBuffer(3, 6)

scala> ArrayBuffer(4, 8).toArray
res134: Array[Int] = Array(4, 8)
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2015 March 15, 09:47