Scala: One more step forward


Hi all,

In two previous posts, I have guided you to prepare the environment, install IDE, and some basic of Scala. You can refer to the first part here, or the second part here

Today, I write the second part of Scala, focus on Arrays, Lists, Maps And Classes

Please note that the original tutorials: http://scalatutorials.com

Arrays

Introduction

  1. Arrays construct
  2. You can declare type of Array like
  3. To print nicely an Array’s content
  4. Arrays are mutable (can’t change it’s size once created, but can modify it’s elements)
  5. Array elements can be of any type

Practice

  1. Write function that print Array elements
  2. Array index
  3. Concatenation
  4. Diff
  5. Contains
  6. And many, many other APi you can find on Scala site: http://www.scala-lang.org/api/current/index.html#scala.Array

List

Introduction

  • Construct
  • List elements can be of any type
  • Reference: http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.List
  • The default List is implemented as a Linked list
  • It is immutable (any “changes” craete a new list, the original is untouched)

Practice

  1. Declare Immutable List
  2. Mutable list
  3. Using (index)
  4. Concatenation
  5. Access elements

Maps

Introduction

  • Maps are constructed simply using Map(key1 -> value1, key2 -> value2, …)
  • The default Map is Predef.Map which points to scala.collection.immutable.Map
  • You can’t have duplicate keys, adding a key value pair whose key already exists, overwrites the value
  • Order of iteration is not guaranteed to be consistent

Practice

  1. Immutable Construction
  2. Mutable Construction
  3. Cannot duplicate key
  4. Access Items
  5. Concatenation
  6. Find elements

Classes

Introduction

  • The class body, is also the default constructor’s implementation
  • Automatic getters are generated for the class parameters defined using val
  • Automatic getters and setters are generated for class parameters defined using var
  • Everything is public by default unless explicity declared otherwise

Practice

Add a Comment

Your email address will not be published.