×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Misc. Topics

Scala Practice

Creating a List in Scala

By IncludeHelp Last updated : October 20, 2024

List is a collection of immutable data of the same type. It represents a LinkedList in Scala.

There are multiple ways to create a list. They are,

  1. General Java-style programming method
  2. Using fill method
  3. Using range method
  4. Using tabulate method
  5. Using lisp style of programming

1. Creating list in Java-style programming method

The general and most popular method to create a list in initializing the elements inside a curly bracket the same style is used to create the list in java, hence it is called the Java-style.

Syntax

val list_name = List(element1, element2,...) 

Example

object MyObject {
    def main(args: Array[String]) {
        println("Creating a List in Java Style")
        val mylist = List(10, 20, 10, 40, 10, 20, 90, 70)
        println("Element of the list:\n" + mylist)
    }
}

Output

Creating a List in Java Style
Element of the list:
List(10, 20, 10, 40, 10, 20, 90, 70)

2. Creating list using fill() method

If you want to create a List of multiple elements with the same data, we will use the fill() method.

Syntax

val list_name = List.fill(count)(element)

Here,

  • count is the number of elements of the List.
  • element is the element that is repeated in the list.

Example

object MyObject {    
    def main(args: Array[String]) {
        println("Creating a List using fill method ")
        val mylist = List.fill(5)("includehelp")
        println("Element of the list:\n" + mylist)
    }
}

Output

Creating a List using fill method 
Element of the list:
List(includehelp, includehelp, includehelp, includehelp, includehelp)

3. Creating list using range() method

If you want to create a List with elements within a given range, we will use the range() method.

Syntax

val list_name = List.range(start, end, step)

Here,

  • start: beginning value of the range.
  • end: end value of the range.
  • step: the step count i.e. element to be skipped in the List.

Example

object MyObject {    
    def main(args: Array[String]) {
        println("Creating a List using range method ")
        val mylist = List.range(10, 50, 5)
        println("Element of the list:\n" + mylist)
    }
}

Output

Creating a List using range method 
Element of the list:
List(10, 15, 20, 25, 30, 35, 40, 45)

4. Creating list using tabulate() method

If you want to create a List with elements that are created passing a set of values to a function.

Syntax

val list_name = List.tabulate(count)(function)

Here,

  • count is count from 0 to count passed to function.
  • function on which the count elements will be passed.

Example

object MyObject {    
    def main(args: Array[String]) {
        println("Creating a List using tabulate method")
        val mylist = List.tabulate(10)(n => n+1)
        println("Elements of the list:\n" + mylist)
    }
}

Output

Creating a List using tabulate method
Elements of the list:
List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

5. Creating list in Lisp style

If you want to create a List using Lisp style programming using ::.

Syntax

    val list_name = element1 :: element2 :: Nil

Nil here is used to end the list.

Example

object MyObject {
    def main(args: Array[String]) {
        println("Creating a List using lisp style ")
        val mylist = "BMW S1000 RR" :: "Thunderbird 350" :: "Iron 883" :: Nil
        println("Elements of the list:\n" + mylist)
    }
}

Output

Creating a List using lisp style 
Elements of the list:
List(BMW S1000 RR, Thunderbird 350, Iron 883)

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.