Social Icons

Monday, June 4, 2018

Kotlin Lists

In the previous chapter we talked about the arrays or arrays and we saw all the potential that it had, but one of the biggest drawbacks was the limitation when defining them, since we had to know in advance the size of said array.

 Types of lists 

The collections can be classified into two large groups, mutable and immutable. That is, those that can be edited (mutable) and those that are only readable (immutable).

 Immutable lists 

The declaration of an immutable list would be like that.
 val readOnly: List < string> = listOf ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
There are several useful functions to work with them.
 val readOnly: List < string> = listOf ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

    readOnly.size // Show the size of the list
    readOnly.get (5) // Returns the value of position 5
    readOnly.first () // Returns the first value
    readOnly.last () // Returns the last value
    println (readOnly) // [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]
As you can see we can work completely with them, except for adding more elements. We can also filter using the iterator pattern, which is a mechanism to access the elements of the list, for example .first() or .last()
val a = readOnly.filter {it == "Monday" || it == "Juernes"}
The .filter allows us to filter in the list through one or several conditions that we put. For this we call it (iterator) and look in the list, if it contains the word "Monday" or "Juernes". In this case, you will only paint "Monday".

 Mutable lists 

Now we have to talk about the most interesting, mutable lists, which have all the above, but also gives us the ability to fill in the list as we need it, the only drawback is that more inefficient memory.
var mutableList: MutableList < string> = mutableListOf ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 

"Saturday")

    println (readOnly) // [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]

    mutableList.add ("Sunday")

The definition is very similar, through the mutableListOf function. Now if you notice I have added from Monday to Saturday what will be painted, then, using mutableList.add I added Sunday. By default the values ​​will be added in the last position, but we can add the index in which we want to write our value.
mutableList.add (0, "Week:")
With this, if we painted the list, it would show us the following.
[Week:, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]
Now we have to be careful with a mutable list for the simple fact that it could be empty or contain a null. A null is a null value in one of its positions, which, if we access it and try to operate, the application will be broken, it will crash. For this, we have some functions that will allow us to work as an immutable list, but with security.
 var mutableList: MutableList < string> = mutableListOf ()
 
    mutableList.none () // We return a true if the list is empty
    mutableList.firstOrNull () // We will return the first field, and if there is not, a null.
    mutableList.elementAtOrNull (2) // The element in index 2, if not, will return a null
    mutableList.lastOrNull () // Last value in the list or null
In this example I have instantiated a list with no value, so it is empty. With all these methods we will recover a null (except the first one that will give true) and the application will continue running, if otherwise we had put a .first() we would have passed the following. We must be very careful when working with data that can change. **Touring lists As we did in the chapter of the arrays, here we also have (very similar) ways to go through the lists. This would be the simplest way, and it would return the contents of each of the values ​​in the list.
  for (item in mutableList) {
        print (item)
    }
If we also need to know the position of each of the values ​​we can use the function .withIndex that allows us to generate 2 variables, the first will be the position and the second the content.
  for ((index, item) in mutableList.withIndex()) {
        println ("The position $ index contains $ item")
    }
For the last example, we will use .forEach, a function that will do one thing for each position, for example, to paint the value as the previous for. Unlike them, we do not have a variable with the content (see index and item) but we access it with the iterator, in this case we simply have to put it. In this example, let's imagine that we want to add two points to the days of the week ":" so we are going to create a new list (mutable) and we will fill in the list through the forEach.
val mutableList: MutableList < string> = mutableListOf ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 

"Saturday")
    val newListEmpty: MutableList < string> = mutableListOf ()

    mutableList.forEach {
        newListEmpty.add (it + ":")
    }

    print (newListEmpty)

No comments:

Post a Comment

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (2) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (189) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (8) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) HTML (7) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (35) Python BeautifulSoup (1) Python For Data Science (2) Python-Books (6) Python-DVD-Training (1) Python-Exercises (165) Python-Framework (1) Python-IDE (1) Python-Modules (1) Python-pdf (2) Python-pyQt (1) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (1) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)
 

Sample text

Sample Text

 
Blogger Templates