Lists
Scala Lists are
quite similar to arrays which means, all the elements of a list have the same
type, but there are two important differences. First, lists are immutable,
which means elements of a list cannot be changed by assignment. Second, lists
represent a linked list whereas arrays are flat.
ListBuffer
ListBuffer is a mutable data structure which allows you to access and modify
elements at specific index. Need to import below object when using ListBuffer.
All methods are same as Lists.
import scala.collection.mutable.ListBuffer
Declaring Lists Variables
To use an
list in a program, you must declare a variable to reference the list and you
must specify the type of list the variable can reference. Here is the syntax
for declaring a list variable:
var
z:List[String] = new List[String]()
You can
assign values to individual elements or get access to individual elements; it
can be done by using commands like the following:
var z =
List("Zara", "Nuha", "Ayan")
// Two
dimensional list
val dim:
List[List[Int]] = List( List(1, 0, 0), List(0, 1, 0), List(0, 0, 1) )
MultiDimensional Lists:
Create Lists of List as needed.
// Create list of nested lists.
val elements = List(List(20, 30),
List(40, 50))
// Print elements.
println(elements)
println(elements.length)
// Access top left element.
val topLeft = elements(0)(0)
println("Top left = " +
topLeft)
// Loop over all elements with a
foreach call.
elements.foreach(_.foreach(println(_)))
Output :
List(List(20, 30), List(40, 50))
2
Top left = 20
20
30
40
50
Processing Lists:
When
processing list elements, we often use either for loop because all of the
elements in a list are of the same type and the size of the list is known. Here
is a complete example of showing how to create, initialize and process lists:
object
Test {
def main(args: Array[String]) {
var myList = List(1.9, 2.9, 3.4, 3.5)
// Print all the list 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 List :
val
test:List[Int] = List(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:List[Int] = List(2,5,3,9,4)
var ans
= testFilter.filter(x => (x % 2 == 0) && (x > 3)) // List(4)
Output:
List(4)
var
result = testFilter.exists(_==50) // if present then it will give 'true' if not it
will give false
println(result)
//false
var
testc:List[Int] = List(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
= List(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 // List(2,5,3,9,4,2,2,4,5,6,6,9)
var
testFilter:List[Int] = List(2,5,3,9,4)
var
testTake=testFilter.splitAt(2)
Output:
testFilter:
List[Int] = List(2, 5, 3, 9, 4)
testTake:
(List[Int], List[Int]) = (List(2, 5), List(3, 9, 4))
val test
= List(10, 20, 30, 40, 10,98,9,8)
var
testTake=test.partition(_>30)
Output:
testTake:
(List[Int], List[Int]) =( List(40, 98), List(10, 20, 30, 10, 9, 8))
val
testdrop = List(10, 20, 30, 40, 10,98)
testdrop.drop(2).map{x=>println(x)}
Output:
30
40
10
98
val
testdrop = List(10, 20, 30, 40, 10,98)
testdrop.dropRight(3).map{x=>println(x)}
Output:
10
20
30
val
testdrop = List(10, 20, 30, 40, 10,98)
var
test=testdrop.filter(_ < 25)
Output:
test:
List[Int] = List(10, 20, 10)
val
testdrop = List(10, 20, 30, 40,
10,98)
var
test=testdrop.filterNot(_ < 25)
Output:
test:
List[Int] = List(30, 40, 98)
val testdrop = List(10, 20, 30, 40, 10,98)
testdrop.head //return testdrop(0)
Integer 10
testdrop.tail //return List(20,30,40,10,98)
all list elements except first one
testdrop.last //return 98
testdrop.slice(3,5) // slice(Starting_Index,Ending_Index-1) return List(40,10)
testdrop.slice(3,5) // slice(Starting_Index,Ending_Index-1) return List(40,10)
val nums
= List(10, 5, 8, 1, 7,8)
val num =
nums.sorted
Output:
num: List[Int] = List(1, 5, 7, 8, 8, 10)
var
test_arr1 : List[Int] = List() // empty Integer List 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: List[Int] = List(17,21)
test_arr1
-= 17
Output :
test_arr1:
List[Int] = List(21)
No comments:
Post a Comment