Listado de la etiqueta: Sets


A set in the Dart programming language is defined as a collection of multiple elements of the same data type. However, each of these elements needs to be unique. In this guide, we will study the usage of the Dart sets in Ubuntu 20.04 by going through some simple yet helpful examples.

Using the Sets in the Dart Programming Language in Ubuntu 20.04:

We have implemented the following seven examples with you to teach you the uses of the sets in the Dart programming language. Each of these examples will introduce you to a new concept associated with the usage of the sets in the Dart programming language.

Example # 1: Declaring a Set by using the Variable Notation in the Dart Programming Language:

There are two different ways of declaring a set in the Dart programming language. We will share with you the first method of doing so. For that, you will have to go through the Dart script shown in the image below:

We have first declared a set named “months” within our “main()” function by making use of the variable notation by using the “var” keyword. This is a “String” type set and contains six different months as its elements. Then, we have just used the “print” statement for printing these elements on the terminal.

To run this Dart script, we have used the following command:

All the elements of our Dart set are shown in the image below:

Example # 2: Declaring a Set by using the Set Notation in the Dart Programming Language:

In this example, we will be sharing with you the second notation for declaring a set in the Dart programming language. For that, you will have to take a look at the following Dart script:

In this Dart script, we have declared the same set of “months” that we have used in the first example. However, this time, we have declared it by using the “set” notation by making use of the “Set” keyword. After declaring and initializing this set, we have just printed its elements on the console.

Upon executing this Dart script, all the elements of our “months” set appeared on the terminal as shown in the image below:

Example # 3: A Set Containing Duplicate Elements in the Dart Programming Language:

Now, when you have learned the two different declarations of the sets in the Dart programming language, we will try to move a step further. We will teach you what happens if you have duplicate elements within a Dart set. For that, you will have to consider the following Dart script:

In this example, we have declared the “months” set in the Dart programming language. However, this time, it contains two duplicate values (i.e. March and September.) To check what happens to this set because of the existence of these duplicate values, we have used a “print” statement for printing the elements of this set on the console.

The output of this Dart script is shown in the image below which tells us that all the duplicate elements present within a Dart set are printed only merienda.

Example # 4: Adding an Element to a Set in the Dart Programming Language:

In this example, we will teach you how you can add an element to an existing set in the Dart programming language. For that, you need to look at the following Dart script:

In this script, we have used the same “months” set that we have used in the examples above. However, this time, we have added a new element to this set by using the “months.add(“May”)” statement. After this modification, we have printed the elements of this set on the terminal again.

The output of this Dart script is shown in the image below. This Dart script has correctly printed the elements of the set after adding a new element to it. Moreover, all the newly added elements to a set are always inserted at the end.

Example # 5: Removing an Element from a Set in the Dart Programming Language:

You can also remove a particular element from a Dart set at any time you want.

In this example, we have just used the “months.remove(“January”)” statement for removing the month “January” from our set. For verifying this removal, we have printed the updated elements of the set on the terminal.

The output of this Dart program is shown in the image below that confirms that our specified element has been successfully removed from our set.

Example # 6: Checking the Length of a Set in the Dart Programming Language:

You can also check the length of a Dart set by making use of the Dart script that follows:

In this Dart program, we have just used the “${months.length}” notation for printing the length of the Dart set on the terminal.

The length of our Dart set in this particular example is “4” as shown in the image below:

Example # 7: Accessing a Particular Index of a Set in the Dart Programming Language:

If you wish to access any particular element or index of a Dart set, then you will have to proceed as explained in the following Dart script:

In this example, we wanted to access the element at the 0th index of our set for which we have simply used the “${months.elementAt(0)}” notation. You can replace “0” with any other index of your choice depending upon the index of the set that you wish to access.

The element present at the 0th index of our set is shown in the image below:

Conclusion:

This article was written to demonstrate the usage of the Dart sets in Ubuntu 20.04. We tried to explain this concept to you with the help of seven simple examples. After going through these examples, you will not only get ordinario with the declaration of the Dart sets but you will also be able to manipulate them by making use of the different associated functions.



Source link