Scala - Create, initialize and process ListBuffer Code Example

The code for Create, initialize and process ListBuffer

import scala.collection.mutable.ListBuffer

object Example {
    def main(args: Array[String]) = {
        var cities = ListBuffer ("New Delhi","Mumbai","Chennai")
        println(cities);
        
        // Add an element
        cities += "Gwalior";
        
        // Add two elements
        cities += ("Bhopal", "Banglore");
        println(cities);
        
        // Remove an element
        cities -= "banglore";
        // print third element
        println(cities(2));
    }
}

/*
Output:
ArrayBuffer(New Delhi, Mumbai, Chennai)
ArrayBuffer(New Delhi, Mumbai, Chennai, Gwalior, Bhopal, Banglore)
Chennai
*/
Code by IncludeHelp, on August 13, 2022 20:58

Comments and Discussions!

Load comments ↻






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