What is the Approach to Using a Mutable List in Scala?
A list in Scala consists of immutable data, which means we can’t change, create or delete it in Scala. From the ListBuffe,r we can access elements at a specific index, then we can add and remove the elements. We can add, change and remove elements in the list by using ListBuffer. After that, convert the ListBuffer to a list when needed.
How does Mutable List use ListBuffer to List in Ubuntu 20.04?
We can use Mutable List with ListBuffer in Scala. Let’s get started to use Mutable List with ListBuffer. From the basics of using the example codes in Scala to access elements from the List, you should take a look at the following examples. All the executions are in Ubuntu 20.04.
Example # 1: Using Mutable List with ListBuffer for Creating Instance:
The example we have is for creating instances of ListBuffer. To use ListBuffer, we have to import scala.collection.mutable.ListBuffer class. Then an Instance is created for ListBuffer.
In the above instance, we have a class called “paint”. The class “paint” defines the main function implementation of the code. In our main function, we have a variable with the keyword “var” which is defined as “color”. The Instance of ListBuffer is declared in the variable “color” with the data type as a string. The ListBuffer is initially empty. We can add elements in the given ListBuffer. Here we are adding elements just by calling the variable name ” color” with the ” +=” operator. Lastly, print the statement by calling a variable “color” in the print function.
The output shows the ListBuffer values in the image below.
Example # 2: Using Mutable List with ListBuffer for Accessing the Elements:
With the help of ListBuffer, we can access the elements the same as the list. For accessing the elements of the ith index, we have to use ListBuffer(i).
In the code example, we have created an object class as “Animal”. The object class has defined the main function. The main function initializes a ListBuffer of data type string in a variable “var” named as “pet”. The instance of ListBuffer is created in the variable “pet”. At first, ListBuffer is empty; we can add the elements by using the “pet” variable as the “+” operator. Here we are using five pet names. To access the index of any pet, we have to give the index value. In the code, we are giving the index value “2nd”. Let’s print the statement to get the index value of the pet.
The 2nd index value from the ListBuffer is “hen” which is shown as the output below.
Example # 3: Using Mutable List with ListBuffer for Adding the Elements:
To append the elements in the list, we have two methods. One is using the “+=” operator, and the other is using ListBuffer.append(). If we want to add or append two or more elements, then the method uses a “varargs” parameter. Let’s execute an example program of this method. We are using both the methods in the code below.
As you can see in the above code, the class object is represented with the name “Health”. The class “fruit” calls the main function for initializing ListBuffer. The variable “var” is defined as “fruits” with a ListBuffer of data type as a string. Now we use the “+=” method and append() method side by side to add elements in the ListBuffer. By printing the statement, you can see the output.
The output ListBuffer has all the elements that we want to append.
Example # 4: Using Mutable List with ListBuffer for Deleting and Removing the Elements:
We can remove or delete the elements from the ListBuffer by using the “-=” method or by using a ListBuffer.remove() method. First, we are taking an example of the “-=” method that will delete elements from the ListBuffer.
Here in the example code above, we have a class “Integer” that defines our main function. In the main function, we have a variable “var” name as “number”. In a variable “number” we have initialized ListBuffer. ListBuffer stores many different values. We can delete them all from the ListBuffer by using the “-=” operator with the variable name “number” like this “number-=”. It will delete the specific number element. After deleting, we can see the ListBuffer through the print function.
We have successfully deleted all the elements from the ListBuffer, as shown in the image below.
Now we are using ListBuffer.remove(). We can remove or delete the element using ListBuffer.remove() method. The method will delete the elements by their position in the ListBuffer. If we want to delete a series of elements, then assign them beginning at the start position and ending index position.
The above code is for removing the elements from class “Values”. As seen in the code, we have assigned different values of ListBuffer in a variable “num”. From these values, we will remove some values by passing the different index values in the num.remove() method. Firstly, we gave the index value 0, and then we gave a start index 2 and the end index 4. After that, we have a print statement to verify that values are removed or not.
The output shows the working of the remove() method as it has removed all the values from the ListBuffer.
The article aimed to give you a way to use Mutable lists through a ListBuffer. As we have executed four different examples with many different methods. We can add, modify, remove, and delete by using mutable lists with the ListBuffer method and converting them into Lists when finished. Hopefully, this article will help you create a mutable list through the ListBuffer method in Scala scripts.