Array
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.
ArrayBuffer
ArrayBuffer is a mutable data structure which allows you to access and modify
elements at specific index. Need to import below object when using ArrayBuffer.
All methods are same as Array.
import scala.collection.mutable.ArrayBuffer
Declaring Array Variables
To use an
array in a program, you must declare a variable to reference the array and you
must specify the type of array the variable can reference. Here is the syntax
for declaring an array variable:
var
z:Array[String] = new Array[String](3)
or
var z =
new Array[String](3)
You can
assign values to individual elements or get access to individual elements; it
can be done by using commands like the following:
z(0) =
"Zara"; z(1) = "Nuha"; z(2) = "Ayan"
or
var z =
Array("Zara", "Nuha", "Ayan")
MultiDimensional Arrays
There are two main solutions:
1.
Use Array.ofDim to create a multidimensional array. You can
use this approach to create arrays of up to five dimensions. With this approach
you need to know the number of rows and columns at creation time.
Eg: var myMatrix = Array.ofDim[Int](3,3)
// build a matrix
for (i <- 0 to 2) {
for ( j <- 0 to 2) {
myMatrix(i)(j) = j;
}
}
// Print two dimensional array
for (i <- 0 to 2) {
for ( j <- 0 to 2) {
print(" " + myMatrix(i)(j));
}
println();
}
Output :
myMatrix: Array[Array[Int]] =
Array(Array(0, 1, 2), Array(0, 1, 2), Array(0, 1, 2))
2. Create arrays of
arrays as needed.
var myMatrix: Array[Array[Int]] = Array(Array(0, 0,
0), Array(0, 0, 0), Array(0, 0, 0))
Processing Arrays:
When
processing array elements, we often use either for loop because all of the
elements in an array are of the same type and the size of the array is known.
Here is a complete example of showing how to create, initialize and process
arrays:
object
Test {
def main(args: Array[String]) {
var myList = Array(1.9, 2.9, 3.4, 3.5)
// Print all the array elements
for ( x <- myList ) {
println( x )
}
// Summing all elements
var total = 0.0;
for ( i <- 0 to (myList.length - 1)) {
total += myList(i);
}
println("Total is " + total);
// Finding the largest element
var max = myList(0);
for ( i <- 1 to (myList.length - 1) ) {
if (myList(i) > max) max = myList(i);
}
println("Max is " + max);
}
}
Output :
1.9
2.9
3.4
3.5
Total is
11.7
Max is
3.5
Methods performed on Array :
val test:Array[Int] =
Array(10,20,30,98,9,8)
test.map{x=>println(x)}
//display
10
20
30
98
9
8
println(test.sum) // addition
Output:
175
println(test.max) // maximum number
Output:
98
println(test.min) // minimum number
Output: 8
println(test.product)// product
Output:
42336000
var
testFilter:Array[Int] = Array(2,5,3,9,4)
var ans
= testFilter.filter(x => (x % 2 == 0) && (x > 3)) // Array(4)
Output:
Array(4)
var
result = testFilter.exists(_==50) // if present then it will give 'true' if not it
will give false
println(result)
//false
var
testc:Array[Int] = Array(2,2,4,5,6,6,9)
testc.distinct.map{x=>println(x)} // it will give the unique value
(2, 4, 5, 6, 9)
val test
= Array(10, 20, 30, 40, 10,98,9,8)
var
testTake = test.take(4)
testTake.map{x=>println(x)}
//it will give (10,20,30,40)
test.takeRight(3) // it will give(98,9,8)
var
res_concat = testFilter ++ testc // Array(2,5,3,9,4,2,2,4,5,6,6,9)
var
testFilter:Array[Int] = Array(2,5,3,9,4)
var
testTake=testFilter.splitAt(2)
Output:
testFilter:
Array[Int] = Array(2, 5, 3, 9, 4)
testTake:
(Array[Int], Array[Int]) = (Array(2, 5),Array(3, 9, 4))
val test
= Array(10, 20, 30, 40, 10,98,9,8)
var
testTake=test.partition(_>30)
Output:
testTake:
(Array[Int], Array[Int]) =(Array(40, 98),Array(10, 20, 30, 10, 9, 8))
val
testdrop = Array(10, 20, 30, 40, 10,98)
testdrop.drop(2).map{x=>println(x)}
Output:
30
40
10
98
val testdrop
= Array(10, 20, 30, 40,
10,98)
testdrop.dropRight(3).map{x=>println(x)}
Output:
10
20
30
val
testdrop = Array(10, 20, 30, 40, 10,98)
var
test=testdrop.filter(_ < 25)
Output:
test:
Array[Int] = Array(10, 20, 10)
val
testdrop = Array(10, 20, 30, 40,
10,98)
var
test=testdrop.filterNot(_ < 25)
Output:
test:
Array[Int] = Array(30, 40, 98)
val
testdrop = Array(10, 20, 30, 40, 10,98)
testdrop.head //return testdrop(0)
Integer 10
testdrop.tail //return Array(20,30,40,10,98)
all array elements except first one
testdrop.last //return 98
testdrop.slice(3,5) //
slice(Starting_Index,Ending_Index-1) return Array(40,10)
val nums
= Array(10, 5, 8, 1, 7,8)
val num =
nums.sorted
Output:
num: Array[Int] = Array(1, 5, 7, 8, 8, 10)
var
test_arr1 : Array[Int] = Array() // empty Integer Array Declaration
//println(test_arr1.length)
test_arr1
= test_arr1 :+ 17 // 0'th index
test_arr1
= test_arr1 :+ 21 // 1'st index
println(test_arr1.length)
//2
Output :
test_arr1:Array[Int]
= Array(17,21)
test_arr1
-= 17
Output :
test_arr1:Array[Int]
= Array(21)
// Updating an element in an array
test_arr1 (0)=24 //Array(24)
// Updating an element in an array
test_arr1 (0)=24 //Array(24)

No comments:
Post a Comment