Scala provides a data structure, the array, which stores a fixed-size
sequential collection of elements of the same type. An array is used to
store a collection of data, but it is often more useful to think of an
array as a collection of variables of the same type. The index of first
element of an array is zero and the last element is the total number of
elements minus one.
There are a few different ways to create String arrays in Scala. If you
know all your array elements initially, you can create a Scala string
array like this:
val fruits = Array("Apple", "Banana", "Orange")
If you don't know the strings that you want in your array initially, but
know the size of your array, you can create it first, then populate it
later, like this:
val fruits = new Array[String](3)
val number = new Array[Int](3)
// somewhere later in the code ...
fruits(0) = "Apple"
fruits(1) = "Banana"
fruits(2) = "Orange"
//
fruits.update(0,"Apple")
fruits.update(1,"Banana")
fruits.update(2,"Orange")
Example1
object Array1 {
def main(args :Array[String])
{
var days = Array("Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday","Saturday" )
println("Array elements are : ")
for ( i <-days )
{
println(i)
}
}
}
Example2
object Array2 {
def main(args :Array[String])
{
var days = new Array[String](7)
days(0)="Sunday"
days(1)="Monday"
days(2)="Tuesday"
days(3)="Wednesday"
days(4)="Thursday"
days(5)="Friday"
days(6)="Saturday"
println("Array elements are : ")
for ( i <-days )
{
println(i)
}
}
}
Update the elements of the arrays
object Array3 {
def main(args :Array[String])
{
var days = Array("Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday","Saturday" )
days(1)="Friday"
println("After update array elements are : ")
for ( i <- days )
{
println(i)
}
}
}
Mutable Scala String arrays
Note that if you want to create a mutable Scala String array, you really
want to use the Scala ArrayBuffer class instead of the Array class,
like this:
import scala.collection.mutable.ArrayBuffer
object MainObject {
def main(args: Array[String]) {
var fruits = ArrayBuffer[String]()
fruits += "Apple"
fruits += "Banana"
fruits += "Orange"
print("Array Elements are: \n")
for(i <- fruits)
{
print(i)
print("\n")
}
}
}
Example4
import scala.collection.mutable.ArrayBuffer
object test{
def main(args: Array[String])
{
var str = ArrayBuffer [String]()
str += "Hi"
str +="Hello"
str +="How are you"
str -="Hi"
println(str)
}
}
Example5
Delete elements for the ArrayBuffer.
We can delete array elements using .remove and -.
import scala.collection.mutable.ArrayBuffer
object demo {
def main(args : Array[String]){
var str = ArrayBuffer [String]()
str += "Hi"
str +="Hello"
str +="How are you"
str.append("Welcome")
str -="Hi"
str.remove(2)
println(str)
}
}
Append elements to an arrays
- :+ append one item to existing Array
- ++ append many items to existing Array
- +: prepend 1 item to existing Array
- ++: prepend many items to existing Array
How to merge Array
object demo {
def main(args : Array[String]){
val a = Array(1,2,3,4,5)
val b = Array(4,5,6,7,8)
val c= a++b
for ( i <- c) {
print(i)
}
println("\n")
val d=Array.concat(a, b)
for ( i <- d) {
print(i)
}
}
}
Concat elements to an arrays
import Array._
object Demo {
def main(args: Array[String]) {
var sname = Array("John","Adam","Rob","Reena","Harry")
var sname1 = Array("Jack","Jill","Henry","Mary","Rohan")
var names = concat( sname, sname1)
println("Student name array elements are : ");
for ( n1 <- names ) {
println( n1 )
}
}
}
Intersect of array elements
object Demo {
def main(args: Array[String]) {
var sname = Array("John","Adam","Rob","Reena","Harry")
var sname1 = Array("Jack","Jill","Henry","John","Mary","Rohan")
var names = sname.intersect(sname1)
println("Student name array elements are : ");
for ( n1 <- names ) {
println( n1 )
}
}
}
Union of array elements
import Array._
object Demo {
def main(args: Array[String]) {
var sname = Array("John","Adam","Rob","Reena","Harry")
var sname1 = Array("Jack","Jill","Henry","John","Mary","Rohan")
var names = sname.union (sname1)
println("Student name array elements are : ");
for ( n1 <- names ) {
println( n1 )
}
}
}
Array With Range
import Array._
object Demo {
def main(args: Array[String]) {
var id = range(7, 23, 3)
var age = range(15,20)
for ( s <- id ) {
print( " " + s)
}
println()
for ( a <- age ) {
print( " " + a )
}
}
}
Multidimensional Array in Scala
To create a multi-dimensional array in Scala, in this case a 2D array (matrix), and then access the array elements.
ofDim method is used for multidimensional query.
object demo
{
def main(args:Array[String]) {
val rows = 2
val cols= 3
val a = Array.ofDim[String](rows, cols)
a(0)(0) = "a"
a(0)(1) = "b"
a(0)(2) = "c"
a(1)(0) = "d"
a(1)(1) = "e"
a(1)(2) = "f"
for{
i <- 0 until rows
j <- 0 until cols
}
println(s"($i)($j) = ${a(i)(j)}")
}
}
// distinct elements from both collectionsscala> val c = a.union(b).distinctc: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)scala> val c = a diff bc: Array[Int] = Array(1, 2, 3)scala> val c = b diff ac: Array[Int] = Array(6, 7, 8)What Are The Differences Between Array And Arraybuffer In Scala?Differences between Array and ArrayBuffer in Scala:
- Array is fixed size array. We cannot change its size once its created.
- ArrayBuffer is variable size array. It can increase or decrease it’s size dynamically.
- Array is something similar to Java’s primitive arrays.
- ArrayBuffer is something similar to Java’s ArrayList.
No comments:
Post a Comment