0% found this document useful (0 votes)
5 views10 pages

Strings-Lists - Week20part3 - Block-A, B, C

This document provides an overview of working with Strings and Lists in Scala, highlighting the use of methods to manipulate these data types. It explains the immutability of Strings, indexing, and various methods available for both Strings and Lists, including creation and manipulation techniques. Additionally, it suggests resources for further reading on Scala programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Strings-Lists - Week20part3 - Block-A, B, C

This document provides an overview of working with Strings and Lists in Scala, highlighting the use of methods to manipulate these data types. It explains the immutability of Strings, indexing, and various methods available for both Strings and Lists, including creation and manipulation techniques. Additionally, it suggests resources for further reading on Scala programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CTEC1703

Computer Programming

Strings and Lists


Week 20 part 3

Introduction to programming in Scala (Block A: week 20)

1
Objectives
 Appreciate how methods can be executed in
Scala

 Further understand Strings and how to utilise


common methods

 Build and process lists of different data types

2
Using methods in Scala
 The Scala library contains methods such as main,
println and readInt that allow us to carry out
different tasks in our program.

 Types (e.g. String) also have methods allowing you


to work with their values. We use the dot . operator to
invoke a method on a variable/value.

var s: String = "fred"


println(s.toUpperCase)
println("JOE".length)
3
Working with Strings
 Scala uses Java's String type and all of its associated
methods, but has also added its own additional
methods in a utility called StringOps.

 Like with other common data structures such as arrays,


Strings utilise an indexing system to determine the
position of each character – indexing starts at zero.

H E L L O
0 1 2 3 4
"HELLO".indexOf("E") //1
"HELLO".charAt(0) //H
4
Common String methods
 There are two overloaded substring methods.
"HELLO".substring(0,2) //HE (2nd argument exclusive)
"HELLO".substring(2) //LLO

 Strings are immutable and several methods return new


strings – this allows operations to be chained. Methods
can also be combined in different ways.

x.substring(3).substring(3).substring(3)
x.substring(0,10).substring(3).toUpperCase
x.charAt(x.indexOf("H")+2)

5
Lists of data
 The List data type allows us to hold a collection of
data of any type, e.g. A list of numbers or strings.

 Lists are simple to create and come with a wealth of


useful methods.
var x: List[Int] = List(1,2,3,4,5)
println(x.sum) //15
println(x.size) //5
println(x.head) //1
println(x.tail) //List(2,3,4,5)
x = 0 :: x //List(0,1,2,3,4,5)
x.foreach(print(_)) //outputs 012345
6
Creating Lists and generating values
 There are a number of ways of constructing a list
containing a predefined set of values.
List(1, 5, 20, 42) //list of integers
List("Apple", "Banana", "Pear") //list of strings
List('a', 'b', 'c') //list of characters

 Lists can also be generated using different functions.

List.range(1,11) //contains 1 - 10
List.fill(3)(5) //contains 5,5,5
List.fill(5)(3) //contains 3,3,3,3,3
List.fill(3)("foo") //contains foo,foo,foo
7
Using List methods
 The List type offers a wealth of methods, e.g.
take – takes elements from start of list
takeRight – takes elements from end of list
drop – drops elements from start of list
dropRight – drops elements from end of list

 Operations can be combined by chaining methods.

x.tail.tail.reverse //List(5,4,3)
x.takeRight(4).take(3).head //2
x.take(4).dropRight(1).tail //List(2,3)

8
Summary
 Strings and Lists offer a wealth of useful methods that
can be used to conduct a variety of tasks and help to
solve common problems.

 Appreciating the index (position) of a character is


important when using certain methods.

 Lists can be generated in a number of different ways to


produce a desired set of predefined values.

9
Suggested reading
 Online Tutorials:
 https://www.tutorialspoint.com/scala/
 http://docs.scala-lang.org/
 https://alvinalexander.com/scala/scala-programming-cookbook
-recipes-faqs

 Textbooks:
 Programming in Scala (3rd edition) by Martin Odersky
 An Introduction to programming and problem-solving using
Scala (2nd edition) by Mark C. Lewis and Lisa L. Lacher

10

You might also like