Open In App

How to access list elements in Scala?

Last Updated : 18 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this articleIn this article, we will learn to access list elements in Scala. List in Scala is the most commonly used collection type, providing an ordered collection that allows accessing elements by index or using functional programming.

How to Access List Elements in Scala?

Below are the possible approaches to access list elements in Scala.

Approach 1: Using for Loop

  1. In this approach, we are using a for loop to iterate through each element of the list.
  2. For each iteration, the loop variable elem represents one element of the list.
  3. Inside the loop, we print each element using the println function, resulting in printing all elements of the list to the console.

In the below example, for Loop is used to access list elements in Scala.

Scala
object GFG {
  def main(args: Array[String]): Unit = {
    val list = List("Geeks", "For", "geeks", "is", "a", "fabulous", "portal")
    for (elem <- list) {
      println(elem)
    }
  }
}

Output:

Screenshot-2024-04-02-at-21-50-43-Scastie---An-interactive-playground-for-Scala

Approach 2: Using 'forEach' method

  1. In this approach, we are using the foreach method on the list to iterate through each element.
  2. The println function is applied to each element, which automatically prints each element of the list to the console.
  3. This method provides a simple way to iterate over the list and perform an action on each element without explicitly using a loop.

In the below example, forEach method is used to access list elements in Scala.

Scala
object GFG {
  def main(args: Array[String]): Unit = {
    val list = List("Geeks", "For", "geeks", "is", "a", "fabulous", "portal")
    list.foreach(println)
  }
}

Output:

Screenshot-2024-04-02-at-21-50-43-Scastie---An-interactive-playground-for-Scala

Approach 3: Using pattern matching and recursion

  1. In this approach, we define a recursive function printList that uses pattern matching to handle the list. If the list is empty (Nil), the function does nothing.
  2. Otherwise, it matches the list into a head element (head) and the remaining elements (tail).
  3. It then prints the head element and recursively calls itself with the tail, effectively printing all elements of the list.
  4. Finally, in the main method, we call printList with the given list to print all its elements.

In the below example, pattern matching and recursion is used to access list elements in Scala.

Scala
object GFG {
  def printList(list: List[String]): Unit = list match {
    case Nil =>
    case head :: tail =>
      println(head)
      printList(tail)
  }

  def main(args: Array[String]): Unit = {
    val list = List("Geeks", "For", "geeks", "is", "a", "fabulous", "portal")
    printList(list)
  }
}

Output:

Screenshot-2024-04-02-at-21-50-43-Scastie---An-interactive-playground-for-Scala


Next Article
Article Tags :

Similar Reads