9/1/2023 0 Comments Kotlin for loop with index![]() ![]() ![]() We can remove element from the start of the LinkedList using removeFirst function. ![]() In the following example, we used addLast method to add Pluto planet to the end of the LinkedList. We can add element to the end of the linked list, using addLast method. In the following example we used addFirst method to add Mercury planet to the top of the LinkedList. We can add element to the start of the linked list, using addFirst method. In the following example, we are retrieving the last element and displaying it. We can get the last element of the LinkedList using last member variable. Println("First planet = " + planets.first) In the following example, we are retrieving the first element and displaying it. We can get the first element of the LinkedList using first member variable. ![]() Planets.addAll(listOf("Earth", "Venus", "Mars")) In the following example, we created a linked list of planets. We can create a linkedlist in Kotlin using. It is handled by the the underlying collections framework. So we can add elements and the size will dynamically increase, and if we remove elements, the size will dynamically reduce. Benefit of linked list is that it is faster to insert and delete an element from LinkedList, it doesn't need rearrangement of entire underlying array. It belongs to the Java collections framework. It implements both the List and Deque interface. In the following example, we will traverse through the "Subject" array and we will print the index along with the value.LinkedList provides a doubly linked list data structure implementation. forEachIndexed is an inline function which takes an array as an input and its index and values are separately accessible. Nstead of using forEach() loop, you can use the forEachIndexed() loop in Kotlin. In this article, we will see how we can access the index of an array in Kotlin while using forEach loop. Sometimes it becomes necessary to access the index of an array. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |