In this section, we will explore two fundamental data structures in Scala: Lists and Arrays. These structures are essential for handling collections of data and are widely used in various programming scenarios.

Lists

Introduction to Lists

  • Immutable: Once created, the elements of a list cannot be changed.
  • Ordered: Elements are stored in a specific sequence.
  • Homogeneous: All elements in a list must be of the same type.

Creating Lists

You can create a list using the List object and specifying the elements:

val fruits = List("Apple", "Banana", "Cherry")

Accessing Elements

You can access elements using the index (starting from 0):

val firstFruit = fruits(0) // "Apple"

Common Operations

  • Head: Returns the first element.
  • Tail: Returns a list of all elements except the first.
  • isEmpty: Checks if the list is empty.
val head = fruits.head // "Apple"
val tail = fruits.tail // List("Banana", "Cherry")
val isEmpty = fruits.isEmpty // false

Practical Example

Let's create a list of numbers and perform some operations:

val numbers = List(1, 2, 3, 4, 5)
val sum = numbers.sum // 15
val doubled = numbers.map(_ * 2) // List(2, 4, 6, 8, 10)

Exercise: List Operations

Task: Create a list of integers and perform the following operations:

  1. Find the maximum value.
  2. Filter out even numbers.
  3. Calculate the product of all elements.

Solution:

val numbers = List(1, 2, 3, 4, 5)
val maxValue = numbers.max // 5
val oddNumbers = numbers.filter(_ % 2 != 0) // List(1, 3, 5)
val product = numbers.product // 120

Arrays

Introduction to Arrays

  • Mutable: Elements can be changed after the array is created.
  • Fixed Size: The size of the array is determined at the time of creation and cannot be changed.
  • Homogeneous: All elements in an array must be of the same type.

Creating Arrays

You can create an array using the Array object and specifying the elements:

val fruits = Array("Apple", "Banana", "Cherry")

Accessing and Modifying Elements

You can access and modify elements using the index:

val firstFruit = fruits(0) // "Apple"
fruits(0) = "Apricot" // Modifies the first element to "Apricot"

Common Operations

  • Length: Returns the number of elements in the array.
  • Foreach: Applies a function to each element.
val length = fruits.length // 3
fruits.foreach(println) // Prints each fruit

Practical Example

Let's create an array of numbers and perform some operations:

val numbers = Array(1, 2, 3, 4, 5)
val sum = numbers.sum // 15
val doubled = numbers.map(_ * 2) // Array(2, 4, 6, 8, 10)

Exercise: Array Operations

Task: Create an array of integers and perform the following operations:

  1. Find the minimum value.
  2. Filter out odd numbers.
  3. Calculate the sum of all elements.

Solution:

val numbers = Array(1, 2, 3, 4, 5)
val minValue = numbers.min // 1
val evenNumbers = numbers.filter(_ % 2 == 0) // Array(2, 4)
val sum = numbers.sum // 15

Comparison: Lists vs. Arrays

Feature List Array
Mutability Immutable Mutable
Size Dynamic Fixed
Performance Good for read-heavy operations Good for write-heavy operations
Syntax List(1, 2, 3) Array(1, 2, 3)

Conclusion

In this section, we have covered the basics of Lists and Arrays in Scala. We learned how to create, access, and manipulate these data structures, and we compared their features and use cases. Understanding these fundamental data structures is crucial for efficient data handling in Scala. In the next section, we will delve into Sets and Maps, which are also essential for managing collections of data.

© Copyright 2024. All rights reserved