Skip to content

Commit eb9c2f4

Browse files
committed
Lists Demo
1 parent 11e7284 commit eb9c2f4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Collections/Lists.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
1. List is companion object
3+
2. List is Immutable object with duplicates allowed, access by index and searchable
4+
3. Nil is an empty list
5+
*/
6+
object Lists extends App
7+
{
8+
//List Declaration
9+
val a = List(1,2,3,4,5)
10+
val a2 = List.apply(1,2,3,4,5)
11+
val a3 = 1::2::3::4::5::Nil
12+
13+
println("a.head = " + a.head) //1. First Element
14+
println("a.tail = " + a.tail) //2,3,4,5 //All Elements other than first one
15+
println("a.init = " + a.init) // All Initial elements except last one
16+
println("a.last = " + a.last) //Last Element in list
17+
18+
println("a(3) = " + a(3)) //4th element in list
19+
println("a.apply(3) = " + a.apply(3)) //4th element in list
20+
21+
println("a.max = " + a.max)
22+
println("a.min = " + a.min)
23+
println("a.isEmpty = " + a.isEmpty)
24+
println("a.nonEmpty = " + a.nonEmpty)
25+
26+
println("a.updated(3,10) = " + a.updated(3,10)) //replace 4th element with 10
27+
28+
println("a.mkString(\",\") = " + a.mkString(","))
29+
println("a.mkString(\"**\") = " + a.mkString("**"))
30+
println("a.mkString(\"[\",\"**\", \"]\") = " + a.mkString("[", "**", "]"))
31+
}

0 commit comments

Comments
 (0)