labands.blogg.se

Kotlin for each index
Kotlin for each index







kotlin for each index

The type of the elements is String, because the values are all strings. The kind of collection is a list, because we used listOf. The type of the elements in the collection is String.These two things together determine the overall type of the collection variable.

kotlin for each index

  • The type of the elements in the collection.
  • The type of the collection we’re using.
  • When working with collections in Kotlin, we have two different types to consider. When you do this, you’ll see its elements in order, like this: Printing a collection variable to the screen. You can also use println() with a collection variable. In the past, we’ve used the println() function to print out the contents of a variable to the screen.
  • Finally, both lists have the titles in a particular order.
  • In Kotlin, the items in a list are called elements*.

    kotlin for each index

    Next, both lists have items in them - in this case, the titles of the books.In Kotlin, the name of the variable holding the list is kind of like the name of the list on paper. The handwritten list and the Kotlin list have a lot in common: The Kingsford Manor Mystery Tea with Agatha Mystery on First Avenue The Ravine of Sorrows Among the Aliens Books to Read val booksToRead = listOf ( "Tea with Agatha", "Mystery on First Avenue", "The Ravine of Sorrows", "Among the Aliens", "The Kingsford Manor Mystery", ) Name of List Items First Last First Last In fact, let’s compare the two! Similarities between Libby's handwritten list and the Kotlin list. This code looks pretty similar to Libby’s handwritten list. Val book5 = "The Kingsford Manor Mystery"Ĭreating a list of strings to represent titles of books. Here’s what she wrote: val book1 = "Tea with Agatha" Libby has been learning to write Kotlin code in her spare time, so she also wanted to write this same list in Kotlin, and print all of the titles to the screen. The Kingsford Manor Mystery Tea with Agatha Mystery on First Avenue The Ravine of Sorrows Among the Aliens Books to Read Here are the titles that are currently on her list: A sheet of paper with a list of books that Libby wants to read. She’s always on the lookout for a great novel, so whenever someone tells her about a good book, she jots down the title on a list that she keeps on a sheet of paper. To learn about collections, let’s visit Libby, a bright young lady who’s always got a book nearby! Who Loves to Read Books? Writing Kotlin becomes so much more interesting once we start putting variables together in a way that we can work on them as a collection. So far, we’ve only worked with variables as individual values.









    Kotlin for each index