score:229
Accepted answer
You can use :+
to append element to array and +:
to prepend it:
0 +: array :+ 4
should produce:
res3: Array[Int] = Array(0, 1, 2, 3, 4)
It's the same as with any other implementation of Seq
.
score:8
The easiest might be:
Array(1, 2, 3) :+ 4
Actually, Array can be implcitly transformed in a WrappedArray
score:65
val array2 = array :+ 4
//Array(1, 2, 3, 4)
Works also "reversed":
val array2 = 4 +: array
Array(4, 1, 2, 3)
There is also an "in-place" version:
var array = Array( 1, 2, 3 )
array +:= 4
//Array(4, 1, 2, 3)
array :+= 0
//Array(4, 1, 2, 3, 0)
Source: stackoverflow.com
Related Query
- Scala: what is the best way to append an element to an Array?
- What is the Best way to loop over an array in scala
- What is the proper way to append an array inside another array in scala
- What is the best way to convert a scala array of strings to a map?
- What is Scala way of finding whether all the elements of an Array has same length?
- What is the best way to check the type of a Scala variable?
- What is the best way to use python code from Scala (or Java)?
- What is the best way to extend a Java Exception in Scala properly?
- What is the best Scala thread-safe way to write to a BufferedWriter?
- What is the preferred way to get the first element of a random-access Scala collection
- What is the best way to assert for a scala method whose return type is Future[Unit]
- What is the best way to write a Scala object to Parquet?
- Best way to insert an element into an ordered array in Scala
- What is the best way to run a Java program that also has Scala code?
- What is the best way modeling in Scala with Spring Data JPA
- What is the functional way to find element by predicate in scala
- What is the best way to merge two Future[Map[T1, T2]] in Scala
- What is the best way to return more than 2 different variable type values in Scala
- What is the best way to find and replace element in immutable set in Scala?
- What is a memory efficient way to convert a Scala Map where the keys are indices to Array efficiently?
- Which is the best way to find element in array column in spark scala?
- What is the efficient way to create Spark DataFrame in Scala with array type columns from another DataFrame that does not have an array column?
- Is there a way to append user input to an array in Scala without overriding my old input every time I call the function?
- What would be the best way in scala to convert between case classes?
- What is the best way to build a model/entity in Scala to map with Squeryl?
- What is the best possible way to call Hadoop FileSystem operations from Serializable Scala object
- What is the best way to store different type of objects in one data structure scala
- Play framework : What is the best way to pass on scala template parameter to Javascript (angular)?
- What is an idiomatic Scala way to "remove" one element from an immutable List?
- What is the idiomatic scala way of finding, if a given string contains a given substring?
More Query from same tag
- Scala - mapping to functions, struggling against contravariance
- Functions vs methods in Scala
- Scala - no data foreach after filter
- How to get the actual type of a generic type?
- How to efficiently calculate/estimate cosine similarity for billions of pairs in a non-spars matrix?
- Spark: stage X contains task of a very large size when running sc.binaryFiles()
- run object scala in databricks
- Thread stuck when use java ConcurrentSkipListSet add method
- trailing asterisks on windows JVM command-line args are globbed in cygwin bash shell
- Monad vs sequential function calls
- Can we improve on my code to swap adjacent array elements in Scala?
- http(/* argument here */) How is this Object (Http) being used without an explicit or implicit method?
- Spark Dataframe GroupBy and compute Complex aggregate function
- Scala - how to validate every String in list using Validated monad from cats?
- Delete logger lines from final jar while performing maven build or package
- Play! 2.1 pass request to another Scala controller
- Flink is not able to serialize scala classes / Task Not Serializable
- project directory for sbt sub-projects
- Style sheets and body and head tags in programmatically produced HTML
- Spark access nested column
- Scala - Confusion with lambda in map function
- Scala: "number" interpolation
- Create an new DenseMatrix from an submatrix in Breeze using Scala
- Spark slow repartitioning many small files
- What are the dangers of using an object as an AKKA actor?
- type mismatch; found : org.apache.spark.sql.DataFrame required: org.apache.spark.rdd.RDD
- How do I convert a java.util.UUID to doobie.syntax.SqlInterpolator.SingleFragment?
- Scala HashMap: iterate in insertion order?
- How to convert an Option[String]to List [Int]in Scala
- How can I get Scala ToolBox to see REPL definitions?